在多线程中对DataGridView指定 DataSource 来填充数据,更新数据的时候,会导致DataGridView出现假死,显示错误或者滚动条无法显示的问题,在保证了DataGridView的ScrollBars设置为了Both,数据量大于DataGridView显示的的范围,而且没有冻结列的情况下,解决方法如下:
使用 Invoke 将执行数据绑定的代码交回给主线程(因为 DataGridView 是主线程创建的); // .net 4.0
this.Invoke(new Action(delegate {
// 绑定数据源
dataGridView1.DataSource = dt;
}));
// .net 2.0
private delegate void InvokeHandler();
this.Invoke(new InvokeHandler(delegate ()
{
// 绑定数据源
dataGridView1.DataSource = dt;
}));