Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 禁用数据网格视图的特定列时,获取空引用错误_C#_Winforms - Fatal编程技术网

C# 禁用数据网格视图的特定列时,获取空引用错误

C# 禁用数据网格视图的特定列时,获取空引用错误,c#,winforms,C#,Winforms,我正在处理windows窗体。。 我有一个数据网格视图,比如(3列)。如果第一列、两列中没有数据,我想禁用第三列。 如果我在前两列中有数据,那么第三列应该是启用的 我的数据网格视图如下 如果前两列有一些数据,那么我必须在第三列中输入一些id。否则,我不想允许在第三列中输入id 因此,我在data gridview cell_clik事件中编写了如下代码: String Cell1=dataGridView1.Rows[0].Cells[0].Value.ToString(); String C

我正在处理windows窗体。。 我有一个数据网格视图,比如(3列)。如果第一列、两列中没有数据,我想禁用第三列。 如果我在前两列中有数据,那么第三列应该是启用的

我的数据网格视图如下

如果前两列有一些数据,那么我必须在第三列中输入一些id。否则,我不想允许在第三列中输入id
因此,我在data gridview cell_clik事件中编写了如下代码:

String Cell1=dataGridView1.Rows[0].Cells[0].Value.ToString();
String Cell2=dataGridView1.Rows[0].Cells[1].Value.ToString();

if(String.IsNullOrWhiteSpace(Cell1) && String.IsNullOrWhiteSpace(Cell2))
{
dataGridView1.Rows[0].Cells[2].ReadOnly = true;
}
所以,当前两列为空时,如果我尝试给出驱动程序id,则在此行中出现错误

String Cell1=dataGridView1.Rows[0].Cells[0].Value.ToString();

对象引用未设置为对象的实例。。那么我没有编写代码的是哪个事件?我的代码怎么了?

当然,如果
DataGridViewCell.Value
为null,则对其调用ToString()会导致异常

你可以把安全检查和

如果要在单元格上启用/禁用行编辑,则需要类似以下内容

protected void dgv_RowEnter(object sender, DataGridViewCellEventArgs  a)
{
    EnableDisableRowCell(dgv.Rows[a.RowIndex]);
}

protected void dgc_CellValueChanged(object sender, DataGridViewCellEventArgs a)
{
    EnableDisableRowCell(dgv.Rows[a.RowIndex]);
}

void EnableDisableRowCell(DataGridViewRow row)
{
    string cell1=row.Cells[0].Value == null ? string.Empty : row.Cells[0].ToString();
    string cell2=row.Cells[1].Value == null ? string.Empty : row.Cells[1].ToString();
    if(string.IsNullOrWhiteSpace(cell1) && string.IsNullOrWhiteSpace(cell2))
        row.Cells[2].ReadOnly = true;
    else
        row.Cells[2].ReadOnly = false;
}

拜托,我还没有在现场项目中再次测试过它。因此,请尝试一下,如果出现问题,请使用调试器查看错误在哪里

sir哪个数据网格视图事件我必须编写此代码?仅数据网格视图CellClick事件我必须编写此代码?这取决于您计划如何与网格交互。在DataBindingComplete中的行上循环可能是另一种选择,或者更好,所以如果我在cellclick事件中编写这段代码,它会正常工作吗?
protected void dgv_RowEnter(object sender, DataGridViewCellEventArgs  a)
{
    EnableDisableRowCell(dgv.Rows[a.RowIndex]);
}

protected void dgc_CellValueChanged(object sender, DataGridViewCellEventArgs a)
{
    EnableDisableRowCell(dgv.Rows[a.RowIndex]);
}

void EnableDisableRowCell(DataGridViewRow row)
{
    string cell1=row.Cells[0].Value == null ? string.Empty : row.Cells[0].ToString();
    string cell2=row.Cells[1].Value == null ? string.Empty : row.Cells[1].ToString();
    if(string.IsNullOrWhiteSpace(cell1) && string.IsNullOrWhiteSpace(cell2))
        row.Cells[2].ReadOnly = true;
    else
        row.Cells[2].ReadOnly = false;
}