Force update DataGridView when editing new row

I was having an issue that a new row that I was editing was not being updated and saving. It would update if I moved to another row, but from a user friendly view this does not work. I don’t want to have the user move to another row so that the update will work.

I tried several things:

dataGridView1.EndEdit();

Didn’t work!

Tried:

bindingSource1.EndEdit();
dataGridView1.EndEdit();

Didn’t work!

Tried:

dataGridView1.EndEdit();
bindingSource1.EndEdit();

Didn’t Work!

After several google searches I came across a mention of useing the BindingContext.

Why is the default behavior for the DataGridView to not update the changes in a new row when save is done, I am at a total loss. I wonder how many developer hours are lost trying to get this to work!

I finally created an extension method as follows:

public static void EndCurrentEdit(this DataGridView dgv) {
  dgv.BindingContext[dgv.DataSource].EndCurrentEdit();
  dgv.EndEdit();
}

To call it I use the following:

dataGridView1.EndCurrentEdit();

Comments are closed.