请问多线程绑定DataGridView应该怎么操作
以前从来没有了解过多线程,下面是我的代码,运行时不稳定,窗体第一遍打开时数据绑定很正常,但当第二次打开窗体时会报错public partial class fm_cust_custinfo : Form
{
DataSet ds_custlist = new DataSet(); //客户列表
Thread thread;
delegate void bindgridview();private void fm_cust_custinfo_Load(object sender, EventArgs e)
{
this.dv_list.AutoGenerateColumns = false;
this.tabControl1.SelectedIndex = 1;
CheckIsIn();
}private void BindList()
{
string strSQL = "select * from cust_custinfo order by filldates desc";
sqlconn.Open();
SqlCommand cmd = new SqlCommand(strSQL, sqlconn);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(ds_custlist);
this.dv_list.DataSource = ds_custlist.Tables[0];
} private void BindThread()
{
ThreadStart bind = new ThreadStart(BindList);
thread = new Thread(bind);
thread.Start();
}private void CheckIsIn()
{
if (this.dv_list.InvokeRequired)
{
bindgridview d = new bindgridview(BindThread);
this.dv_list.Invoke(d);
}
else
{
BindThread();
}
}}
希望各位不吝赐教,小弟先谢过了
以前从来没有了解过多线程,下面是我的代码,运行时不稳定,窗体第一遍打开时数据绑定很正常,但当第二次打开窗体时会报错public partial class fm_cust_custinfo : Form
{
DataSet ds_custlist = new DataSet(); //客户列表
Thread thread;
delegate void bindgridview();private void fm_cust_custinfo_Load(object sender, EventArgs e)
{
this.dv_list.AutoGenerateColumns = false;
this.tabControl1.SelectedIndex = 1;
CheckIsIn();
}private void BindList()
{
string strSQL = "select * from cust_custinfo order by filldates desc";
sqlconn.Open();
SqlCommand cmd = new SqlCommand(strSQL, sqlconn);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(ds_custlist);
this.dv_list.DataSource = ds_custlist.Tables[0];
} private void BindThread()
{
ThreadStart bind = new ThreadStart(BindList);
thread = new Thread(bind);
thread.Start();
}private void CheckIsIn()
{
if (this.dv_list.InvokeRequired)
{
bindgridview d = new bindgridview(BindThread);
this.dv_list.Invoke(d);
}
else
{
BindThread();
}
}}
希望各位不吝赐教,小弟先谢过了
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货