0

I have a datagridview and a label with text "Fetching data ...". some data is fetched from remote sql server database which probably takes some time. I want to show this "fetching data ..." label till the data is loaded.

my code on button click is:

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;

i tried background worker but it gives cross thread error.

  • See [How to update the GUI from another thread in C#?](http://stackoverflow.com/q/661561/119477) You should note that some answers are framework dependant – Conrad Frix Aug 11 '15 at 17:34
  • @frix Thank you so much my friend.. i tried to build the code using this idea.. but unfortunately its again not working.. something is missing.. – Divyanshu Kumar Aug 12 '15 at 04:33
  • 1
    you should [Edit] your question to include what you tried and what you mean by "not working" – Conrad Frix Aug 12 '15 at 15:56

1 Answers1

0

Bind the handler

backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted; 

Handler method

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{

       label1.Visible = false;
}
zawhtut
  • 8,335
  • 5
  • 52
  • 76