Loading a DataGridView with a BackgroundWorker with the 2.0 Framework.
Posted on 2007-12-18 20:03 礼拜一 阅读(438) 评论(2) 编辑 收藏 举报
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
using (SqlConnection cn = new SqlConnection(ConnectString))
{
SqlCommand cmd = new SqlCommand("SELECT * FROM CUSTOMERS", cn);
dt = new DataTable("Customers");
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
}
private void btnLoadData_Click(object sender, EventArgs e)
{
worker.RunWorkerAsync();
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//Will be called when the DoWork is completed
LoadData();
}
private void LoadData()
{
//No exceptions here.
dataGridView1.DataSource = dt;
}
{
using (SqlConnection cn = new SqlConnection(ConnectString))
{
SqlCommand cmd = new SqlCommand("SELECT * FROM CUSTOMERS", cn);
dt = new DataTable("Customers");
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
}
private void btnLoadData_Click(object sender, EventArgs e)
{
worker.RunWorkerAsync();
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//Will be called when the DoWork is completed
LoadData();
}
private void LoadData()
{
//No exceptions here.
dataGridView1.DataSource = dt;
}
http://msmvps.com/blogs/williamryan/archive/2004/12/10/23367.aspx