Progress bar count percentage not updating correctly win c#

Gani_tpt 1,766 Reputation points

I am trying to apply progress bar. here percentage updating not correctly. what is the problem.

progbar1.Maximum = dt.Rows.Count; // Total Records 1000  
progbar1.Minimum = 0;  
progbar1.Step = 3;    
foreach (DataRow DR in dt.Rows)  

Here, the initial progress percentage showing "57%". But, it support to start 0%.

While debugging it's showing "0%". but, in the front end progress bar showing "57%".

How it is possible...?


An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,389 questions
0 comments No comments
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,621 Reputation points

    Hi GaniTPT,
    Based on your code, there is a problem that the value of progbar1 will exceed his maximum value in the foreach loop.
    And to avoid blocking the UI thread, I suggest you use BackgroundWorker to update the progress.
    Here is a code example you can refer to.

    private void button1_Click(object sender, EventArgs e)  
        progressBar1.Maximum = dt.Rows.Count;  
        progressBar1.Value = 0;  
        progressBar1.Step = 3;  
        progressBar1.Minimum = 0;  
        backgroundWorker1.WorkerReportsProgress = true;  
    DataTable dt = new DataTable();  
    private void Form1_Load(object sender, EventArgs e)  
        for (int i = 0; i <= 1000; i++)  
            DataRow row = dt.NewRow();  
    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)  
        progressBar1.Value = e.ProgressPercentage;  
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)  
        var backgroundWorker = sender as BackgroundWorker;  
        for (int j = 0; j <= dt.Rows.Count; j++)  
            backgroundWorker.ReportProgress((j * progressBar1.Maximum) / dt.Rows.Count);  

    Best Regards,
    Daniel Zhang

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

2 additional answers

Sort by: Most helpful
  1. Viorel 113.1K Reputation points

    I think that your program cannot handle the Paint event and update the appearance because it is busy with the loop or is stopped at breakpoint.

    As a workaround, try executing progbar1.Update() inside the loop. This usually helps in case of standard Progress Bar.

    0 comments No comments

  2. Karen Payne MVP 35,201 Reputation points


    Full source is in this project on GitHub.

    • First example uses a standard ProgressBar with cancellation option (written a while ago)
    • Second example uses a custom Progressbar with percentage shown.
    • Both examples keep the user interface responsive
    • For the second example, there is a await Task.Delay(50), for real life usage change to Task.Delay(1). 50 was used to allow you to see progress and try and move the form around showing all is responsive.

    Here I will focus on the custom progress bar.

    private async Task ProgressbarExperiment()  
        ProgressBarSpecial.Value = 0;  
        var maxValue = 10000;  
        ProgressBarSpecial.Maximum = maxValue;  
        for (int index = 0; index < maxValue; index++)  
            int value = ProgressBarSpecial.Value + 100;  
            await Task.Delay(50);  
            if (value > ProgressBarSpecial.Maximum)  
                ProgressBarSpecial.Value = ProgressBarSpecial.Maximum;  
                ProgressBarSpecial.Value = value;  
        await Task.Delay(1000);  
    private async void RunButton1_Click(object sender, EventArgs e)  
        await ProgressbarExperiment();  




    0 comments No comments