1
using System.Threading;
先定义委托 线程
Thread myThread;
private delegate void myDelegate(string r, string str);//定义委托
2
窗体载入时添加列
private void Form1_Load(object sender, EventArgs e)
{
this.dataGridView1.Columns.Add("ad", "ad");//添加列
}
3
private void button1_Click(object sender, EventArgs e)
{
myThread= new Thread(startFillDv);//实例化线程
myThread .Start();//启动线程
}
4
private void startFillDv()
{
for (int i =0;i<=50000;i++)
{
Grid(i.ToString(), i.ToString());
Thread.Sleep(1000);//干这么久,休息下的
}
}
5
private void Grid(string s,string sa)
{
if (this.InvokeRequired)
{
this.Invoke(new myDelegate(Grid), new object[] { s, sa });
}
else
this.dataGridView1.Rows.Add(sa); //加行
}
完整代码: