locked
How to use progress bar/spinner when data is loading in datagridview? RRS feed

  • Question

  • Hello, I`m making a windows application

    When I click the Button then data is loading in Datagridview,

    In this step I want to show the progress using Progress bar or spinner.

    But for me, this is the first time to use that, I don`t have no idea...

    Please somebody help me.

    My button click event is below. how can I add or modify code?

    private void btnStart_Click(object sender, EventArgs e)
            {
                string webApplicationName = string.Empty;
                string siteID = string.Empty;
                mtlblError.Text = "no record returned";
    
                Migration_Status_DAC dac = new Migration_Status_DAC();
                DataSet ds = new DataSet();
    
                try
                {
                            
                    ds = dac.SelectWebApplicationStatus(mtextUrl.Text);
                    DataTable dt = ds.Tables[0];
    
    
                    if (ds != null && dt != null && dt.Rows.Count > 0)
                    {
                        webApplicationName = dt.Rows[0]["AppName"].ToString();
                        mgrdWebApplication.DataSource = dt;
                    }
                    else
                    {
                         mtlblError.Visible = true;
                    }
                }
                catch(Exception ex)
                {
                    LogWrite.writeLog(ex.ToString();
                }
            }

    Tuesday, October 11, 2016 1:22 AM

Answers

  • Hi RydenChoi,

    Are you download data from networking ? In general, creating a new thread to handle the time-

    consuming operations like downloads and database transactions for avoiding UI thread blocked cause your application stopped responding. If you don't have experience about multi-thread, link below can help you to understand the asynchronous programming in C#.

    BackgroundWorker Class 

    System.Threading Namespace

    Asynchronous Programming with async and await (C#)

    and I make an example in backgroundworker control to show a progress for your requirements. Code below is for your reference.

            BackgroundWorker backgroundWorker;
            private void Form1_Load(object sender, EventArgs e)
            {
                backgroundWorker = new BackgroundWorker();
                progressBar1.Visible = false;
                progressBar1.Style = ProgressBarStyle.Marquee;
                backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
                backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_Completed);
            }
            private delegate void updateProgressDelegate();
    
            private void updateProgressBar()
            {
                progressBar1.Visible = true;
            }
            private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
            {
                this.Invoke(new updateProgressDelegate(updateProgressBar));
                //call your code here 
                
            }
            private void backgroundWorker_Completed(object sender, RunWorkerCompletedEventArgs e)
            {
                progressBar1.Visible = false;
            }
            private void button1_Click(object sender, EventArgs e)
            {
                if(!backgroundWorker.IsBusy)
                backgroundWorker.RunWorkerAsync();
            }

    I hope this can be helpful.

    Best Regards,

    Bob


    • Edited by Bob Ding Tuesday, October 11, 2016 6:37 AM
    • Marked as answer by RydenChoi Wednesday, October 12, 2016 4:32 AM
    Tuesday, October 11, 2016 6:35 AM

All replies

  • Hi RydenChoi,

    Are you download data from networking ? In general, creating a new thread to handle the time-

    consuming operations like downloads and database transactions for avoiding UI thread blocked cause your application stopped responding. If you don't have experience about multi-thread, link below can help you to understand the asynchronous programming in C#.

    BackgroundWorker Class 

    System.Threading Namespace

    Asynchronous Programming with async and await (C#)

    and I make an example in backgroundworker control to show a progress for your requirements. Code below is for your reference.

            BackgroundWorker backgroundWorker;
            private void Form1_Load(object sender, EventArgs e)
            {
                backgroundWorker = new BackgroundWorker();
                progressBar1.Visible = false;
                progressBar1.Style = ProgressBarStyle.Marquee;
                backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
                backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_Completed);
            }
            private delegate void updateProgressDelegate();
    
            private void updateProgressBar()
            {
                progressBar1.Visible = true;
            }
            private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
            {
                this.Invoke(new updateProgressDelegate(updateProgressBar));
                //call your code here 
                
            }
            private void backgroundWorker_Completed(object sender, RunWorkerCompletedEventArgs e)
            {
                progressBar1.Visible = false;
            }
            private void button1_Click(object sender, EventArgs e)
            {
                if(!backgroundWorker.IsBusy)
                backgroundWorker.RunWorkerAsync();
            }

    I hope this can be helpful.

    Best Regards,

    Bob


    • Edited by Bob Ding Tuesday, October 11, 2016 6:37 AM
    • Marked as answer by RydenChoi Wednesday, October 12, 2016 4:32 AM
    Tuesday, October 11, 2016 6:35 AM
  • Oh thanks your comment Bob!

    I tried to use your code, and add my code in doWork method, then click the button,

    But not worked... So I debugged my code, and I found backgroundWorker.IsBusy value is false

    How can i fix that???

    My code is totally same on your code, just put in my code in doWork method...

    Please help me

    Wednesday, October 12, 2016 1:22 AM
  • Hi RydenChoi,

    I just make an example to show how do you use the backgroundworker, and it may not be entirely suitable for you. It is hard to give you a all of the available solution for you because the click event code snippet you shared is not a available executable code for me. 

    >> backgroundWorker.IsBusy value is false

    where do you find the isBusy value is false? If it is in the button click event, then the condition will be established. and the workerAsync() method will be executed, this method will create a background thread to call the DoWork() method.

    In addition, If you want to update the datagridview which create by UI thread in the background thread, you should use a delegate to update the datagridview like progressBar.

    Best Regards,

    Bob


    • Edited by Bob Ding Wednesday, October 12, 2016 2:07 AM
    Wednesday, October 12, 2016 2:06 AM
  • Thank you Bob

    Because this is my first time to use BackgroundWokrer, it feels really hard...

    But your code and links helped me.

    I`m not completed my code yet, but at least, I knew how to use and handle it.

    Thanks your advice!


    • Edited by RydenChoi Wednesday, October 12, 2016 5:40 AM
    Wednesday, October 12, 2016 5:39 AM