设置DataGridView.DataSource异步 [英] Set DataGridView.DataSource async

查看:659
本文介绍了设置DataGridView.DataSource异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须说我一直在读这个网站很久,总是找到我在找的东西。不幸的是,今天不是这样的。



我有一个.NET C#Winforms应用程序,它使用MySQL .NET Connector查询数据库并填写DataGridView的结果。 / p>

这是我的代码:

  using(var SqlConn = new MySqlConnection (MyConnectionString)
{
using(var SqlComm = new MySqlCommand(SELECT * FROM my_database.city,SqlConn)
{
using(var SqlAdapter = new MySqlDataAdapter(SqlComm)
{
DataTable SqlOutput = new DataTable();
SqlAdapter.Fill(SqlOutput);
myDataGridView.DataSource = SqlOutput;
}
}
}

代码正常工作,问题是,考虑到表有20K行,需要一些时间加载,同时阻止UI。



做一些测试,我得出结论,数据库查询填充()它非常快(实际上需要)像10ms)。 因此,设置DataGridView.DataSource属性会延迟整个操作



所以我的问题是,有没有办法设置DataSource特权异步?或者也许是在数据加载时我仍然可以使用表单的方式?



我也想知道是否有更好的方法来做,因为我会每次在数据库表上进行更改时都会调用此方法,因此我可以显示用户更新的信息。



提前感谢

解决方案

文档建议使用virualmode属性设置。



https://msdn.microsoft .com / en-us / library / 15a31akc.aspx?cs-save-lang = 1& cs-lang = csharp#code-snippet-1



还有其他有趣的话题即时加载。
在Windows窗体中实现带有即时数据加载的虚拟模式DataGridView控件



https://msdn.microsoft.com/en-us/library/ms171624(v = vs.110).aspx



您在上述链接中有很好的例子。值得看着他们。


I must say I've been reading this site for a long time and always found what I was looking for. Sadly, today is not the case.

I have a .NET C# Winforms Application that uses MySQL .NET Connector to query a database and fill a DataGridView with the results.

Here is my code:

using (var SqlConn = new MySqlConnection(MyConnectionString)
{
    using (var SqlComm = new MySqlCommand("SELECT * FROM my_database.city", SqlConn)
    {
        using (var SqlAdapter = new MySqlDataAdapter(SqlComm)
        {
            DataTable SqlOutput = new DataTable();
            SqlAdapter.Fill(SqlOutput);
            myDataGridView.DataSource = SqlOutput;
        }
    }
}

The code is working fine. The problem is that, considering that the table has like 20K rows, it takes some time to load and in the meantime, it blocks UI.

Doing some tests, I concluded that the database query Fill() its very fast (it actually takes like 10ms). Therefore, setting DataGridView.DataSource property delays the whole operation.

So my question is, Is there a way to set DataSource propery async? Or maybe a way where I can still use the form while the data is loading?

I was also wondering if there is a better way of doing this, since I would be calling this method every time I do changes on the database table, so I could show the user updated information.

Thanks in advance.

解决方案

Documentation suggests to go with "virualmode" property setting.

https://msdn.microsoft.com/en-us/library/15a31akc.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

There is other interesting topic "Just in time loading". Implementing Virtual Mode with Just-In-Time Data Loading in the Windows Forms DataGridView Control

https://msdn.microsoft.com/en-us/library/ms171624(v=vs.110).aspx

You have good examples in above link. Worth looking at them.

这篇关于设置DataGridView.DataSource异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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