我在我的表单中有一个winform Datagridview控件在加载时我填充了近1000条记录。每隔几秒就从我的sql server获取数据,并且必须在网格上填充数据。我不希望我的网格冻结。请帮帮我......
答案 0 :(得分:0)
答案 1 :(得分:0)
只需使用计时器,当它打勾时创建一个新线程并更新DataGrid
。
Windows窗体和TPL(.NET Framework 4和3.5)的示例。
初始化计时器:
this.timer = new Timer() { Interval = 2000 };
this.timer.Tick += OnFetch;
this.timer.Start();
管理Tick事件:
private void OnFetch(object sender, EventArgs e)
{
Task.Factory.StartNew(() =>
{
// Get the data from your db
var data = GetDbData();
foreach (MyElement row in data)
{
if (dataGridView1.InvokeRequired)
{
dataGridView1.Invoke(new Action(() =>
{
// Add the row
}));
}
}
});
}