如果您有一个绑定(bind)到 DataView (someDataTable.DefaultView) 的 DataGridView。
..并通过代码对底层 DataTable 中的行执行了一些编辑。
是否可以推迟 DataGridView 的更新,直到您决定完成对行的编辑?
事实上,DataGridView 在每次编辑后都会更新,如果您不需要即时反馈,那么如果您一个接一个地更新 DataTable 中的许多行,这将是低效的并且在视觉上有点不协调。
最佳答案
为了能够暂时中止数据绑定(bind),您必须输入 BindingSource
在您的 DataGridView
和您的 DataView
之间。通过将 BindingSource
的 RaiseListChangedEvents
属性设置为 false,底层源中的更改不会通知到 DataGridView
。您可以从设计 View 的工具箱中拖放一个 Bindingsource
组件。我尝试通过设计器设置数据源,但没有用,所以我用代码实现了:
bindingSource1.DataSource = someDataTable.DefaultView;
dataGridView1.DataSource = bindingSource1;
要暂停数据绑定(bind),只需将 RaiseListChangedEvents
属性设置为 false:
bindingSource1.RaiseListChangedEvents = false;
要恢复数据绑定(bind),只需将 RaiseListChangedEvents
设置为 true 并重置绑定(bind)以便更新显示:
bindingSource1.RaiseListChangedEvents = true;
bindingSource1.ResetBindings(false);
关于c# - 编辑底层 DataTable 时延迟 DataGridView 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1132818/