DataTable table;int currentIndex = 0;int max = 10000;private void button1_Click(objectsender, EventArgs e)
{
buttonOK.Enabled= false;
Thread thread= new Thread(newThreadStart(LoadData));
thread.IsBackground= true;
thread.Start();
progressBar1.Minimum= 0;
progressBar1.Maximum=max;
}private voidLoadData()
{
SetLabelText("数据加载中...");
currentIndex= 0;
table= newDataTable();
table.Columns.Add("id");
table.Columns.Add("name");
table.Columns.Add("age");while (currentIndex
{
SetLabelText(string.Format("当前行:{0},剩余量:{1},完成比例:{2}%",
currentIndex, max- currentIndex, (Convert.ToDecimal(currentIndex) / Convert.ToDecimal(max) * 100).ToString()));
SetPbValue(currentIndex);
DataRow dr=table.NewRow();
dr["id"] =currentIndex;
dr["name"] = "张三";
dr["age"] = currentIndex + 5;
table.Rows.Add(dr);
currentIndex++;
}
SetDgvDataSource(table);
SetLabelText("数据加载完成");this.BeginInvoke(new MethodInvoker(delegate()
{
buttonOK.Enabled= true;
}));
}delegate void labDelegate(stringstr);private void SetLabelText(stringstr)
{if(label1.InvokeRequired)
{
Invoke(new labDelegate(SetLabelText), new string[] { str });
}else{
label1.Text=str;
}
}delegate voiddgvDelegate(DataTable table);private voidSetDgvDataSource(DataTable table)
{if(dataGridView1.InvokeRequired)
{
Invoke(new dgvDelegate(SetDgvDataSource), new object[] { table });
}else{
dataGridView1.DataSource=table;
}
}private delegate void pbDelegate(intvalue);private void SetPbValue(intvalue)
{if(progressBar1.InvokeRequired)
{
Invoke(new pbDelegate(SetPbValue), new object[] { value });
}else{
progressBar1.Value=value;
}
}