c#中使用线程 在Datagridview 中如何显示上线,左右滚动条
1. 定义
Thread app;
2.使用 在按钮 或者load中
if (app != null)
{
app.DisableComObjectEagerCleanup();
app.Abort();
}
Control.CheckForIllegalCrossThreadCalls = false;
app = new Thread(InitData);// 调用的函数名称为InitData
app.SetApartmentState(ApartmentState.STA);
app.Start();
3.如果窗口关闭,在窗口关闭的事件中,添加如下代码,将线程关闭
private void FShouHuoTongJiChaXun_FormClosing(object sender, FormClosingEventArgs e)
{
if (app != null)
{
app.DisableComObjectEagerCleanup();
app.Abort();
}
}
4.在有线程定义的界面中,DataGridview不能显示左右、上下滚动条。
在开始 定义
private delegate void InvokeHander();
在数据处理过程中。增加以下语句
this.Invoke(new InvokeHander(
delegate()
{
this.dataGridView1.DataSource = null;
this.dataGridView1.DataSource = dt;
}
));
这样处理后,就可以显示了。具体原因,请大家各抒己见。
优质内容筛选与推荐>>