使用 C# Windows 窗体中 SQL 查询的大量结果填充 dataGridView

发布于 2024-10-08 03:01:51 字数 853 浏览 6 评论 0原文

我有一个使用 dataGridView 创建的 Windows 窗体。我还有一个长时间运行的 SQL 查询,正在 BackgroundWorker 中运行线程填充静态数据表。

private void RunQuery_DoWork(object sender, DoWorkEventArgs e)
{
    OdbcDataAdapter adapter = new OdbcDataAdapter(longRunningSQLQuery, datasourcename);
    adapter.Fill(results);
}

private void RunQuery_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    dataGridView1.DataSource = results;
}

这工作得很好。当我去运行查询时,UI 保持响应,因为结果是在后台生成的...但是,当需要在 dataGridView 中显示结果时,如果它是大量数据,窗口会冻结。如果我让它静置一段时间,最终它就会完成。在我的 RunWorkerCompleted 回调函数中,我调用 dataGridView1.DataSource = results; (结果是我的数据集),这是需要很长时间的部分。

有什么方法可以预先绑定 dataGridView 或将其绑定在后台工作人员中吗?

I have a Windows Forms created with a dataGridView on it. I also have a long running SQL query that I am running in a BackgroundWorker thread to populate a static datatable.

private void RunQuery_DoWork(object sender, DoWorkEventArgs e)
{
    OdbcDataAdapter adapter = new OdbcDataAdapter(longRunningSQLQuery, datasourcename);
    adapter.Fill(results);
}

private void RunQuery_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    dataGridView1.DataSource = results;
}

This works perfectly fine. When I go to run the query, the UI remains responsive as the results are generated in the background... However, when it comes time to display the results in my dataGridView the window freezes if it's a large set of data. If I let it sit for a while, eventually it finishes. In my RunWorkerCompleted callback function, I call
dataGridView1.DataSource = results; (results is my DataSet) and this is the part that is taking a long time.

Is there any way I can pre-bind the dataGridView, or bind it in the backgroundworker?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

喜爱纠缠 2024-10-15 03:01:51

如果您使用数据网格的虚拟模式,则仅加载显示的行。否则,将加载所有行。

http://msdn.microsoft.com/en-us/library/2b177d6d.aspx

If you use the Virtualmode of the datagrid, only the displayed rows are loaded. Otherwise, all rows will be loaded.

http://msdn.microsoft.com/en-us/library/2b177d6d.aspx

无声情话 2024-10-15 03:01:51

它将使用反射从数据源中提取值。更快的方法可能是自己构建和添加行。您也许可以在后台线程中构建行列表,然后在主 UI 线程上将它们添加为范围 - 不确定。

显然,这样做会失去数据绑定的好处,但如果您想加载大量数据,这可能是唯一的选择。事实证明,您可以使用 VirtualMode,您自己实现部件,但保留数据绑定的好处。

It will use reflection to pull the values out of the data source. A faster way may be to build and add the rows yourself. You might be able to build a list of rows in the background thread, and then on the main UI thread add them as a range - not sure.

Obviously doing this you lose the benefits of data-binding, but if you want to load a mammoth amount of data, this could be the only option. Turns out you can use VirtualMode, you implement parts yourself but keep the benefits of data-binding.

爱人如己 2024-10-15 03:01:51

由于 GridView 是一个可视组件,我认为规则适用,您只能从创建这些控件的前台线程中调用方法和属性。

也许您可以分步填充网格,例如每次迭代添加 100 条记录,使控件响应更顺畅。

Microsoft SQL Server Management Studio似乎也使用GridView,可以很好地处理大行。

Since a GridView is a visual component, I think the rule applies, that you can call the methods and properties only from within the foreground thread that created those controls.

Maybe you can do a filling of the grid in steps, adding each e.g. 100 records per iteration, making the control respond more smoothly.

Microsoft SQL Server Management Studio seems to use a GridView, too, the handle large rows rather well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文