dimitarie
dimitarie

Reputation: 437

Updating a database record after datagridview cell value has been changed?

we used to use a farpoint grid which made this quite easy but now we're using datagridview for our forms. My problem is I need to update a record as it is changed in the datagridview, I used the Cell Leave event.

private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
    {
        if (dataGridView1[e.ColumnIndex, e.RowIndex].Value == null)//incase it is a blanked cell
        {
            MessageBox.Show("Cell changed:" + e.ColumnIndex.ToString() + ":" + e.RowIndex.ToString() + " New Value:" + " null");

        }
        else
        {
            MessageBox.Show("Cell changed:" + e.ColumnIndex.ToString() + ":" + e.RowIndex.ToString() + " New Value:" + dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString());

        }
    }

I test the above code by changing a value, and then pressing enter key (just like the users do currently) but it shows not the changed value but the previous one.

A workaround that I've already done is update all the records within the datagridview, problem is it takes about 15 more seconds than previously which I don't want - plus it's unnecessary if one record is changed only.

Upvotes: 2

Views: 4141

Answers (1)

dimitarie
dimitarie

Reputation: 437

Okay, I think I might have found my answer: If i put the following code before the if-else statement then the cell changes have been recognized

    dataGridView1.EndEdit();

After that, the changed cell values are seen..looks good!

Upvotes: 1

Related Questions