优化更新数据表绑定到的DataGridView [英] optimize updates to DataTable bound to DataGridView

查看:296
本文介绍了优化更新数据表绑定到的DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示一些数据在我的应用程序的表单。当我第一次展示的形式,我加载一些数据到一个DataTable然后将DataTable绑定到一个DataGridView。我也开始能执行某些较慢的数据库查询异步方法。当这些慢查询完整的,我需要更新DataTable中几百行,填补了从慢查询返回的值,例如:

 的foreach(DataRow的行data.Rows)
{
    SlowLoadingData slow_stuff = slow_query_results [(INT)行[ID]];

    row.BeginEdit();
    行[column_one] = slow_stuff.One;
    行[column_two] = slow_stuff.Two;
    行[column_three] = slow_stuff.Three;
    row.EndEdit();
}
 

这是极其缓慢的,挂一分钟或以上,presumably UI线程,因为每一行触发重绘。

经过一番研究,我发现了一个方法,使之快。首先,绑定的DataGridView来绑定到DataTable一个BindingSource的,而不是直接到DataTable。然后,执行如下当您更改数据表:

  binding_source.SuspendBinding();
binding_source.RaiseListChangedEvents = FALSE;
//的foreach(DataRow的在Data.Rows)... code以上
binding_source.RaiseListChangedEvents = TRUE;
binding_source.ResumeBinding();
grid.Refresh();
 

还有一个问题,虽然,这是一个谎言:在code以上$ P $从检测添加到DataTable新行pvents DataGridView中。 添加到表中的任何新行不会出现在网格中。电网还可以抛出异常,如果您使用箭头键移动当前单元格选择离网的底端,因为基础数据源有更多的行,但电网尚未创建网格行来显示它们。

所以,两种可能的解决方案,我可以看到:

  1. 有没有更好的方式来燮preSS绑定更新,同时在更改基础DataTable?

  2. 有没有一种简单的方法来告诉DataGridView中优雅地刷新其网格行集合匹配的基础数据表的行数? (注:我已经打过电话BindingSource.ResetBindings,但似乎如果你有引发更多的异常的删除的从数据表行)

解决方案

你有没有考虑断开数据网格或BindingSource的却使表,之后重新连接?它看起来有点难看,但它应该是快了很多。

I have a Form in my application that displays some data. When I first show the Form, I load some data into a DataTable then bind the DataTable to a DataGridView. I also start an asynchronous method that executes some slower database queries. When these slow queries complete, I need to update a few hundred rows in the DataTable, filling in values returned from the slower queries, like so:

foreach (DataRow row in data.Rows)
{
    SlowLoadingData slow_stuff = slow_query_results[(int)row["id"]];

    row.BeginEdit();
    row[column_one] = slow_stuff.One;
    row[column_two] = slow_stuff.Two;
    row[column_three] = slow_stuff.Three;
    row.EndEdit();
}

This is extremely slow, hanging the UI thread for a minute or more, presumably because each row is triggering a redraw.

After some research, I found a way to make it fast. First, bind the DataGridView to a BindingSource that is bound to the DataTable, instead of directly to the DataTable. Then, do as follows when you make changes to the DataTable:

binding_source.SuspendBinding();
binding_source.RaiseListChangedEvents = false;
// foreach (DataRow in Data.Rows) ... code above
binding_source.RaiseListChangedEvents = true;
binding_source.ResumeBinding();
grid.Refresh();

There is a problem, though, and it's a doozy: the code above prevents the DataGridView from detecting new rows added to the DataTable. Any new rows added to the table do not appear in the grid. The grid may also throw exceptions if you use the arrow keys to move the current cell selection off the bottom end of the grid, because the underlying data source has more rows but the grid has not created grid rows to display them.

So, two possible solutions that I can see:

  1. Is there a better way to suppress binding updates while making changes to the underlying DataTable?

  2. Is there an easy way to tell the DataGridView to gracefully refresh its grid row collection to match the number of underlying DataTable rows? (Note: I've tried calling BindingSource.ResetBindings, but it seems to trigger more exceptions if you have removed rows from the DataTable!)

解决方案

Have you considered disconnecting the dataGrid or the bindingSource while filling the table and reconnecting afterwards? It might look a bit ugly, but it should be a lot faster.

这篇关于优化更新数据表绑定到的DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆