Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deferring DataGridView update when editing the underlying DataTable

If you have a DataGridView that is bound to a DataView (someDataTable.DefaultView).

..and a number of edits are performed on rows in the underlying DataTable from code.

Is it possible to defer the updating of the DataGridView until you decide that you are finished editing rows?

As it is, the DataGridView is updated after every edit, which, if you don't require instant feedback, is inefficient and a little visually jarring if you are updating many rows in the DataTable one after the other.

like image 262
xyz Avatar asked Jul 15 '09 17:07

xyz


1 Answers

In order to be able to temporarily suspend data binding, you'll have to put a BindingSource between your DataGridView and your DataView. By setting the RaiseListChangedEvents property of the BindingSource to false, changes in the underlying source are not notified to the DataGridView. You can drag & drop a Bindingsource component from the toolbox in the design view. I tried to set up the data sources via the designer but it didn't work, so I did it in code:

bindingSource1.DataSource = someDataTable.DefaultView;
dataGridView1.DataSource = bindingSource1;

To suspend data binding, just set the RaiseListChangedEvents property to false:

bindingSource1.RaiseListChangedEvents = false;

To resume data binding, just set the RaiseListChangedEvents to true and reset the bindings so the display is updated:

bindingSource1.RaiseListChangedEvents = true;
bindingSource1.ResetBindings(false);
like image 186
Julien Poulin Avatar answered Oct 17 '22 11:10

Julien Poulin