How to show progress bar while data fetch from db in c#?

Farshad Valizade 201 Reputation points

Hi my friends. my topic is duplicate and I have seen like this much in the web but my code is different from others.

I have a win form project that is 3Layers .

In view I call a method from BLL and It returns data from DAL and show to a Datagridview in UI.

 private void Form_Load(object sender, EventArgs e){
private Void GetData(){
Datagridview.Datasource = LineBLL.GetAll();

How can I show Progress Bar or Loading Animation before GetData() and disappear it after the data load.

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.
8,969 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 10,381 Reputation points Microsoft Vendor

    Hi @Farshad Valizade , Welcome to Microsoft Q&A,

    You can use await Task.Run to start the data retrieval task asynchronously.

    Before getting data, set the style and scroll speed of the progressBar.

    I use Datatable to demonstrate here. When using it, you need to pay attention to changing Form1_Load to async.

    using System.Data;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    namespace _9_14_x
         public partial class Form1 : Form
             public Form1()
             private DataTable dt;
             private async void Form1_Load(object sender, System.EventArgs e)
                 // Create DataTable
                 dt = new DataTable("MyTable");
                 dt.Columns.Add("ID", typeof(int));
                 dt.Columns.Add("Ok", typeof(bool));
                 //Set the progress bar to scroll style
                 progressBar1.Style = ProgressBarStyle.Marquee;
                 //Scroll speed: the smaller, the faster 1-100
                 progressBar1.MarqueeAnimationSpeed = 1;
                 //Show progress bar or loading animation
                 progressBar1.Visible = true;
                 // Call the GetData method asynchronously
                 await GetDataAsync();
                 //Hide the progress bar or load animation
                 progressBar1.Visible = false;
             private async Task GetDataAsync()
                 // Use Task.Run to perform time-consuming operations in the background thread
                 // var data = await Task.Run(() => LineBLL.GetAll());
                 //Simulate asynchronous loading of data, simulating 2 seconds of loading time
                 await Task.Delay(2000);
                 // Clear old data
                 //Add new data
                 dt.Rows.Add(1, true);
                 dt.Rows.Add(2, false);
                 dt.Rows.Add(3, true);
                 dt.Rows.Add(4, false);
                 //After the data is loaded, bind the data to the DataGridView
                 dataGridView1.DataSource = dt;

    enter image description here

    Best Regards,


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    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.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Johan Smarius 390 Reputation points

    You could use the Task Parallel Library (TPL) for this. Be careful not to update the UI in the Task code, because WinForms should only be updated from the main thread.

    User's image

    0 comments No comments