Link to home
Start Free TrialLog in
Avatar of burningmace
burningmaceFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Adding a progress bar to a DataGridView in .NET

I'm trying to add a progress bar to a DataGridView, but it seems impossible. I've seen a few rather poor examples that I can't get working, and the custom controls that do it all seem to be expensive and I don't want to pay for one.

I'm populating the DGV manually and not from a database, and I need to be able to re-order and resize the columns and rows, as well as update the bar in real time. I see this feature commonly in applications, it seems odd that Microsoft chose not to add a simple ProgressBarCell class.

Thanks in advance.
SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Can you give a little more information?

First suggestion would be to handle the OnDataBinding event of the GridView and then depending on how you are manually creating the data, Stepping through the Progress bar accordingly.



Grid_OnDataBinding(object sender, EventArgs e)
{
for(loop conditions
{
...
ProgressBar.PerformStep();
}
}

Open in new window

Avatar of burningmace

ASKER

When I tried that class none of the grid lines were drawn and the rest of my text in the DGV flickered horribly. Am I doing something wrong?
I didn't try that example myself, since I have no need, but there might be other examples to be found for creating a custom column with a progress bar that wouldn't flicker.
I've been looking for one for ages, and I still can't find anything useful. I see this function in hundreds of programs (to name a few: uTorrent, Xilisoft Video Converter, iTunes, Photoshop, Illustrator, Wavelab) and I can't believe that it is such a hard task to make a progress bar in a DGV that doesn't flicker or look crappy.
Sometimes, you can hit a boundary for what is reasonable to ask of a control, and I can believe that is a hard task for the DataGridView, since you have to jump through a lot of hoops just to add a custom column type.
True, but the demand for one to be included as a column type in .NET is high, so I'm surprised that MS haven't done it already. Plus I don't see why so many other programs can do it and there's no simple solution when a developer wants to add one to their program.
I don't think that Windows Forms is Microsoft's priority right now, so I understand and I am not surprised.  I don't think the demand is high enough to move that mountain.

I am assuming that you have tried double-buffering to reduce flickering (I didn't try that example code)...
For some reason the calls to Paint for each cell take an age to complete. I timed the difference, and it's roughly 14ms per cell when it's not processing a progress cel, and 60ms+ on a cell that draws progressl. This seems ludicrous - a refresh rate on cells of only 71Hz. On a 6 column DGV with one progress column and 10 rows, it took a little over 1.1 seconds to draw the entire thing. Upon resizng a column, the DGV flickers so badly that you can't see the contents.
I use the Infragistics UltraWinGrid/UltraWebGrid, which has a progress bar column type, so I don't have a need to use the DataGridView, but I do feel your pain...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The overflow is because the rectangle bounds are calculated from ChunkThickness:

       rect.Width = (int)value * ProgressBarRenderer.ChunkThickness;
 
       ProgressBarRenderer.DrawHorizontalBar(graphics, rect);
       ProgressBarRenderer.DrawHorizontalChunks(graphics, rect);

This can be tightened down to get a more exact measurement.  The example code was just a proof-of-concept.