简体   繁体   中英

DataGridView cell is null on CellLeave

I am trying to do some text processing on the contents of the cell when the cell is left. I have the following code, but I get the following exception when I enter any text into the cell and then leave it.

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

Additional information: Object reference not set to an instance of an object.

If I break and mousehover above .value it is indeed null, but I have entered data into the cell! So what gives?

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

The value of a cell is in a transient state when the DataGridView CellLeave event fires. This is because DataGridView may be bound to a datasource and the change will not have been commited.

Your best option is to use the CellValueChanged event.

Add some checks:

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

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

I think you want to handle CellEndEdit rather than CellLeave.

On CellLeave, an edited cell's Value property is still unchanged (ie null for an empty cell as you observed by breaking and inspecting Value). On CellEndEdit, its new value has been set.

Try something like this, wherein I have tried to generally stick to your original code:

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

I think you are leaving a blank cell and trying to process its value.

when you will leave a blank cell value of following code:

string entry = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();

value in the string entry will be blank space(entry="") and when you are processing this value further by passing it to another function[ MakeTextFeet(entry);] it is giving you error.

Solution from my point of view for this problem is>>>

put each line of code in above method also and MakeTextFeet(Entry) also in try block.

When you will write catch block leave that block empty. Eg.

try
{
.
.
.
}
catch(Exception)
{
}

By this thing your exception will naturally get caught but since its nigligible, it will not show you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM