Another way of doing this, is to use a
DataGridView
control and to use a custom class that derives
DataGridViewTextBoxCell
to implement the ProgressBar painting as illustrated below. The
DataGridViewProgressColumn
class that uses the
DataGridViewProgressCell
can be added to the
Columns
collection of the grid. Then bind this column to a field providing the progress values in your datasource. You then have the option to also paint any text over the ProgressBar.
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
public class DataGridViewProgressCell : DataGridViewTextBoxCell
{
public DataGridViewProgressCell()
{
}
protected override void Paint(Graphics graphics,
Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
DataGridViewElementStates cellState, object value,
object formattedValue, string errorText, DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, string.Empty,
string.Empty, string.Empty, cellStyle, advancedBorderStyle, paintParts);
double progress = (double)value;
VisualStyleRenderer backgoundRenderer = new VisualStyleRenderer(VisualStyleElement.ProgressBar.Bar.Normal);
if (backgoundRenderer != null)
{
backgoundRenderer.DrawBackground(graphics, clipBounds);
Rectangle rBar = new Rectangle(clipBounds.X + 1, clipBounds.Y + 1, Convert.ToInt32((progress * clipBounds.Width)) - 2, clipBounds.Height - 2);
VisualStyleRenderer barRenderer = new VisualStyleRenderer(VisualStyleElement.ProgressBar.Chunk.Normal);
if (barRenderer != null)
{
barRenderer.DrawBackground(graphics, rBar);
}
}
}
}
public class DataGridViewProgressColumn : DataGridViewTextBoxColumn
{
public DataGridViewProgressColumn()
{
base.CellTemplate = new DataGridViewProgressCell();
}
}