-1

enter image description here

I wanted to make textbox column single cell readonly to false based on checkbox. if a checkbox is checked then rextbox would be writable.

        private void dgvItem_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        { bool valid = Convert.ToBoolean(this.dgvItem.CurrentRow.Cells[0].Value.ToString()); //null reference exception
            if(valid)
            {
                this.dgvItem.CurrentRow.Cells[3].ReadOnly = false;
            }
        }

s checkd then cell would writable. but i get

null reference exception everytime

  • 1
    This is a duplicate of: [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp May 14 '20 at 22:56

1 Answers1

0

CurrentRow could be null, or the cell value could be null. Use the following code

private void dgvItem_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    //if column 0 is the checkbox column, then execute the code only for it
    if (e.ColumnIndex == 0) 
    {
        bool valid = Convert.ToBoolean(Convert.ToString(this.dgvItem.Rows[e.RowIndex].Cells[e.ColumnIndex].Value));
        this.dgvItem.Rows[e.RowIndex].Cells[3].ReadOnly = !valid;
    }
}