Add progress bar in gridview using datatable or dataset in window application

后端 未结 2 1609
無奈伤痛
無奈伤痛 2020-12-17 06:24

I need to add a progress bar in a DataGridView using DataTable or DataSet in a WinForms application similar to this:

2条回答
  •  暖寄归人
    2020-12-17 07:07

    There is no such thing as DataGridViewProgressColumn. To get an actual ProgressBar column will take some work - that is a ProgressBar that is embedded inside a cell...

    The simplest way to achieve something like what you want is to have an animated .gif file within the relevant cell, so in this case you would use a DatGridViewImageColumn and select the required animate .gif as the image using a `PictureBox'.

    To do smoothly will require multi-threading the process that you wish to show the progress of. The basic outline of how to do this would be

    1. Choose your animated .gif file from this amazing site.

    2. Import a Timer into your form and set the timer interval as '50'.

    3. Create a picture box on you form and set its size to "1, 1" (very small) and the image source to your animated .gif.

    4. Select this PictureBox as the DatGridViewImageColumn source image.

    Then include the following code

    private void yourTimerForGifAnimation_Tick(object sender, EventArgs e)
    {
        this.dataGridView1.Rows[someRow].Cells["My Progress Bar Column"].Value = 
            this.pictureBoxGif.Image            
        this.dataGridView1.InvalidateCell(cellColumnIndex, cellRowIndex);
    }
    

    This will create your animated progress image. Now all you need to worry about is not doing work on the UI thread as this will cause the animation to free along with everything else!

    I hope this helps.

提交回复
热议问题