DataGridView 单元格在 CellLeave 上为空

Dav*_*vid 5 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.
Run Code Online (Sandbox Code Playgroud)

如果我打破并将鼠标悬停在上面.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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Ste*_*ens 1

添加一些检查:

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

if (MyCell != null)
{
    if (MyCell.Value != null)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)