我想根据 C# 数据网格视图中的复选框列使单个文本框单元格可读



在此处输入图像描述

我想根据 复选框。如果选中复选框,则 Rextbox 将是可写的。

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选中,然后单元格将可写。 但我得到

每次都出现空引用异常

CurrentRow 可以为空,或者单元格值可以为空。 使用以下代码

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;
}
}

最新更新