首页  :: 新随笔  :: 订阅 订阅  :: 管理

 

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;
}




http://msmvps.com/blogs/williamryan/archive/2004/12/10/23367.aspx