编辑底层DataTable时推迟DataGridView更新

问题描述:

如果您有一个绑定到DataView(someDataTable.DefaultView)的DataGridView。编辑底层DataTable时推迟DataGridView更新

..并且从代码对底层DataTable中的行执行了大量编辑。

是否可以推迟更新DataGridView,直到您决定完成编辑行为止?

实际上,每次编辑后都会更新DataGridView,如果您不需要即时反馈,则DataGridView效率低下,并且在DataTable中依次更新多行时会产生一些视觉震动。

为了能够暂时中止数据绑定,您必须在您的DataGridViewDataView之间放置BindingSource。通过将BindingSourceRaiseListChangedEvents属性设置为false,底层源的更改不会通知DataGridView。您可以在设计视图中将&拖放到工具箱中的Bindingsource组件。我试图建立通过设计师的数据来源,但它没有工作,所以我做到了在代码:

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

暂停数据绑定,只是RaiseListChangedEvents属性设置为false:

bindingSource1.RaiseListChangedEvents = false; 

要恢复数据绑定,只需设置RaiseListChangedEvents为true,并重新绑定,因此更新显示:

bindingSource1.RaiseListChangedEvents = true; 
bindingSource1.ResetBindings(false); 
+0

尼斯的答案。谢谢。 – xyz 2009-07-15 20:51:41