C#dataGridView 多线程加载大量数据 带进度条显示 +图片动态加载中
C#dataGridView 多线程加载大量数据 带进度条显示 +图片动态加载中
private void button9_Click(object sender, EventArgs e)
{
pictureBox1.Visible = true;
Control.CheckForIllegalCrossThreadCalls = false; // 多线程在控件
ThreadStart ts = new ThreadStart(function);
Thread t = new Thread(ts);
t.Start();
}
}
private void function()
{
string strConn = label27.Text; ///这里是数据库连接
SqlConnection con = new SqlConnection(strConn);
SqlDataAdapter da = new SqlDataAdapter("select * from shangpin", con);
DataSet ds = new DataSet();
da.Fill(ds);
int count = ds.Tables[0].Rows.Count;
progressBar2.Minimum = 0;
progressBar2.Maximum = 100;
progressBar2.BackColor = Color.Green;
for (int i = 0; i < count; i++) //为了能看出效果 用for循环来减低速度
{
progressBar2.Value = ((i + 1) * 100 / count);
}
dataGridView2.DataSource = ds.Tables[0];
pictureBox1.Visible = false ; ///显示完后 隐藏
}