3

Here Mark Rideout, a Microsoft developer explains how to create a progress bar column in a DataGridView, and provides a "bare-bone" code for DataGridViewProgressColumn class. His instructions on how to use it for databound datagridviews are:

If you are databound, then you can just change the column type of an integer column that has 0 through 100 values.

Some people, including myself do not understand what does it mean to "change the column type of an integer column that has 0 through 100 values" in the case with databound grid, and Mark seems to be too busy to answer. Does anyone know how it is done?

Here is a quick sample of my case:

namespace Sample
{
    public class Record
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Progress { get; set; } // This is the int 0-100 field which 
                                          // has data for a progress bar

        public Record()
        {
        }

        public Record(int id, string name, int progress)
        {
            this.Id = id;
            this.Name = name;
            this.Progress= progress;
        }
    }
}

namespace Sample
{
    public partial class SampleForm : Form
    {
        private BindingList<Record> records;

        public SampleForm()
        {
            InitializeComponent();
        }

        public SampleForm(BindingList<Record> records)
        {
            InitializeComponent();
            this.records = records;
        }

        private void SampleForm_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = this.records;
        }
    }
}

At what point exactly should I change the "Progress" column type to DataGridViewProgressColumn, and how is that done?

pnuts
  • 58,317
  • 11
  • 87
  • 139
OctoRazor
  • 31
  • 1
  • 1
  • 3

3 Answers3

8

As I understand you need to bind such type of columns to the properties which have type Int32 and can have values from 0 to 100 (as percentage).

Data entry class:

class Data
{
    public int Progress { get; set; }
}

Binding of the data:

var data = new List<Data>() { 
    new Data() { Progress = 50 },
    new Data() { Progress = 60 },
    new Data() { Progress = 30 },
    new Data() { Progress = 92 },
};

this.dataGridView1.DataSource = data;

** This is how it looks: **

Class, described in the article should be added to the project a built before adding columns to GridView.

And don't forget that allowed values only from 0 to 100.

Steps

enter image description here

enter image description here

enter image description here

Samich
  • 29,157
  • 6
  • 68
  • 77
  • Could you please provide an example which will work for the databound scenario listed above? – OctoRazor Sep 02 '11 at 23:08
  • sure.. moment please... just checked :) – Samich Sep 02 '11 at 23:14
  • Samich, this sounds stupid, I know... but how does your Column1 know that its type should be DataGridViewProgressColumn? – OctoRazor Sep 02 '11 at 23:26
  • You can select the type of column in *Edit Column* dialog. Couple sec please, I'll post an image – Samich Sep 02 '11 at 23:29
  • BTW, it's quiet buggy solution.. :) After adding such column you may have problems with forms designer. – Samich Sep 02 '11 at 23:30
  • But my columns are autogenerated, how do I get to that dialog? (thanks a lot for your help) – OctoRazor Sep 02 '11 at 23:30
  • With auto-generated columns it will not work (or at lease I don't know how) – Samich Sep 02 '11 at 23:39
  • I think I figured it out! Thank you very much! – OctoRazor Sep 02 '11 at 23:39
  • There is no such thing build-in as "DataGridViewProgressColumn". But you can get the class for it from https://social.msdn.microsoft.com/Forums/windows/en-US/769ca9d6-1e9d-4d76-8c23-db535b2f19c2/sample-code-datagridview-progress-bar-column?forum=winformsdatacontrols or an updated version from https://github.com/jimradford/superputty/blob/master/SuperPutty/Gui/DataGridViewProgressColumn.cs – frzsombor Sep 01 '17 at 22:55
0

I use VB.NET the attached Link is in C# anyway, so its going to be easy for you to Understand. Once you include the two classes, just call the Namespace in you form, build your solution, then add a datagridview control and add a column of type DataGridViewProgressColumn to the control. You can thus use the following code to populate the control.

DataGridView1.Rows.Add(20)
DataGridView1.Rows.Add(20)

P.S My code sample is in VB but am sure you can figure out it's equivallent for C#

Dennis Henry
  • 76
  • 10
0

This works for me, so maybe this will help someone...

progressBar1.Bounds = dgv.GetCellDisplayRectangle(columnIndex, rowIndex, true);
Animateur
  • 1
  • 1