在winforms中从服务器数据库中获取数据时显示加载文本

时间:2015-08-11 17:13:26

标签: c# winforms datagridview

我有一个datagridview和一个带有文本“获取数据......”的标签。 从远程sql server数据库获取一些数据可能需要一些时间。我希望在加载数据之前显示“获取数据...”标签。

按钮点击的代码是:

label1.Visible = true;
        String connection = "Data Source = 19xx.xx.xx; Initial Catalog =xxx; integrated security = false ; User ID=XXX; Password=XXX";
        String query = "select * from sys.tables";
        SqlConnection con = new SqlConnection(connection);
        SqlDataAdapter ad = new SqlDataAdapter(query, con);
        DataTable dt = new DataTable();
        ad.Fill(dt);
        dataGridView1.DataSource = dt;
        label1.Visible = false;

我尝试了后台工作程序,但它给出了交叉线程错误。

1 个答案:

答案 0 :(得分:0)

绑定处理程序

backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted; 

处理程序方法

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{

       label1.Visible = false;
}