C# CellLeave上的DataGridView单元格为空

C# CellLeave上的DataGridView单元格为空,c#,datagridview,visual-studio-2012,C#,Datagridview,Visual Studio 2012,当单元格离开时,我试图对单元格的内容进行一些文本处理。我有下面的代码,但当我在单元格中输入任何文本并将其保留时,会出现以下异常 An unhandled exception of type 'System.NullReferenceException' occurred in Program.exe Additional information: Object reference not set to an instance of an object. 如果我在.value上面打断并鼠标悬停

当单元格离开时,我试图对单元格的内容进行一些文本处理。我有下面的代码,但当我在单元格中输入任何文本并将其保留时,会出现以下异常

An unhandled exception of type 'System.NullReferenceException' occurred in Program.exe

Additional information: Object reference not set to an instance of an object.
如果我在
.value
上面打断并鼠标悬停,它确实是空的,但我已经在单元格中输入了数据!那么是什么原因呢

private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 3)
    {
        string entry = "";
        MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
        MakeTextFeet(entry);
    }
    if (e.ColumnIndex == 4)
    {
        string entry = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
        MakeTextFeet(entry);
    }
}

我认为您正在留下一个空白单元格,并试图处理其值

将保留以下代码的空白单元格值时:

string entry = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
字符串条目中的值将为空白(条目=“”) 当您通过将该值传递给另一个函数[MakeTextFoots(entry);]来进一步处理该值时,它会给您带来错误

从我的观点来看,这个问题的解决方案是>>>

将每行代码也放在上面的方法中,并将MakeTextFeet(条目)也放在try块中

编写catch块时,请将该块留空。 例如

通过这种方式,您的异常自然会被捕获,但由于它很小,因此不会显示给您。

添加一些检查:

DataGridViewCell MyCell =  dataGridView1[e.ColumnIndex, e.RowIndex];

if (MyCell != null)
{
    if (MyCell.Value != null)
    {
    }
}

我想你想处理CellEndEdit而不是Celled Leave

在CellLeave上,已编辑单元格的Value属性仍保持不变(即,通过断开和检查值观察到的空单元格为null)。在CellEndEdit上,已设置其新值

试着这样做,我试着坚持你的原始代码:

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex];

    if (e.ColumnIndex == 3 || e.ColumnIndex == 4)
    {
        string entry = "";
        if (cell.Value != null)
        {
            entry = cell.Value.ToString();
        }
        MessageBox.Show(entry);
        MakeTextFeet(entry);
    }
}

当DataGridView CellLeave事件触发时,单元格的值处于瞬态。这是因为DataGridView可能已绑定到数据源,并且更改尚未提交


您最好的选择是使用事件。

我认为问题在于此方法MakeTextFoots(entry)您是否将调试器放在该代码上?我也有同样的问题-我离开行的单元格从未显示值,尽管输入了值。同一行中的其他单元格正确显示该值@pgfearo提出了一个很好的观点。你的选择取决于MakeTextFoots在做什么。如果只需要对生成更改值的编辑进行操作,请处理CellValueChanged。如果它需要在任何情况下对编辑进行操作(例如,在必要时将“'”附加到值),请处理CellEndEdit。@Steve Wellens也提出了一个很好的观点。您需要检查值是否为空。即使在先前输入的值上退格,CellEndEdit和CellValueChanged处理程序中也会产生空值。不,他不会留下空白单元格。我也有同样的问题-我在单元格中输入文本,但在rowleave事件中,单元格为空。空单元格是我离开该行的单元格,尽管我已在该单元格中输入了文本。同一行中的其他单元格显示文本!
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex];

    if (e.ColumnIndex == 3 || e.ColumnIndex == 4)
    {
        string entry = "";
        if (cell.Value != null)
        {
            entry = cell.Value.ToString();
        }
        MessageBox.Show(entry);
        MakeTextFeet(entry);
    }
}