Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dataset contains 7 lakhs records. if we want to load the data into datagridview.it can take the some processing. In that time i will raise the progress bar with continuously to after conpletion of the data to datagridview.

But here is i wrote the code, it is correct or not and any other process can do it.

C#
private void btnSubmit_Click(object sender, EventArgs e)
        {

           string connstring = "Data Source=IICSS75;User ID=pmisdbadmin;Password=pmisdbadmin;Initial Catalog= PMA23052013";

            SqlConnection con = new SqlConnection(connstring);

            SqlDataAdapter da = new SqlDataAdapter("Select * from ATTENDANCE", con);

            DataSet ds = new DataSet();
            da.Fill(ds);

           

            dataGridView1.Visible = true;
           

            progressBar1.Visible = true;
            progressBar1.Show();

            progressBar1.Minimum = 1;
            progressBar1.Value = 1;
            progressBar1.Step = 1;
           
            progressBar1.Maximum = ds.Tables[0].Rows.Count;

            for (int i = 1; i < ds.Tables[0].Rows.Count; i++)
            {
                progressBar1.PerformStep();

                  x = 1;
                

            }
            if (x == 1)
            {
                dataGridView1.DataSource = ds;
            }
Posted
Updated 6-Aug-13 20:52pm
v2

You can't do it like that: The Fill method is a blocking call, which means it doesn't return until it is completed. As a result, it prevents you updates to the progress control from happening.

The best solution to this is to move the slow operation into a different thread, which allows your main (UI) thread to continue updating the display while the operation is going on. Have a look at the BackgroundWorker class[^] - it's designed for just this, and the link includes an example.
 
Share this answer
 
Comments
adriancs 7-Aug-13 4:25am    
I always use BackgroundWorker to perform these tasks.
OriginalGriff 7-Aug-13 4:35am    
So do I - I hate sluggish UIs! :laugh:
The problem is that you're doing all the work on the UI thread. This means that you update the value that the progress bar should display, but you never give it a chance to redraw itself with the new value. It also means that your application will appear to hang while the data is being populated into the grid.

To overcome this problem, move that functionality out to a different method, then execute the method on a new thread. Because the new method will be running on a background thread, it may not interact with the GUI. Therefore, you will need to create another new method to report the progress. This method will need to be invoked on the GUI thread.

Some links to help you get started:
http://www.csharpque.com/2012/04/progressbar-example-cracked-easy.html[^]
http://www.csharp-examples.net/asynchronous-method-progress/[^]
http://www.albahari.com/threading/part3.aspx[^]
Progress Reporting in C# 5 Async[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900