Creiamo una classe chiamata DataGridViewProgressCell
che si occuperà di disegnare la progressbar ed eredita dalla classe DataGridViewImageCell, ed una classe DataGridViewProgressColumn che eredita dalla classe DataGridViewImageColumn.
DataGridViewProgressCell.cs
namespace Morpheusweb.Controls
{
public class DataGridViewProgressCell : DataGridViewImageCell
private float _maxValue = 100
private Color _fillingColor = Color.FromArgb(21, 232, 30)
private bool _drawText
public bool DrawTex
{
get { return _drawText; }
set { _drawText = value; }
}
public Color FillingColor
{
get { return _fillingColor; }
set { _fillingColor = value; }
}
public float MaxValue
{
get { return _maxValue; }
set { _maxValue = value; }
}
private static Image emptyImage;
static DataGridViewProgressCell()
{
emptyImage = new Bitmap(1, 1,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
}
public DataGridViewProgressCell()
{
ValueType = typeof (int);
}
public override sealed Type ValueType
{
get { return base.ValueType; }
set { base.ValueType = value; }
}
protected override object GetFormattedValue(object value,
int rowIndex, ref DataGridViewCellStyle cellStyle,
TypeConverter valueTypeConverter,
TypeConverter formattedValueTypeConverter,
DataGridViewDataErrorContexts context)
{
return emptyImage;
}
protected override void Paint(Graphics g, Rectangle clipBounds,
Rectangle cellBounds,
int rowIndex, DataGridViewElementStates cellState,
object value, object formattedValue, string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
[...]
}
}
}
DataGridViewProgressColumn.cs
namespace Morpheusweb.Controls
{
public class DataGridViewProgressColumn : DataGridViewImageColumn
{
float _maxValue;
Color _fillingColor = Color.Green;
bool _drawText;
public bool DrawText
{
get { return _drawText; }
set { _drawText = value; }
}
public Color FillingColor
{
get { return _fillingColor; }
set { _fillingColor = value; }
}
public float MaxValue
{
get { return _maxValue; }
set { _maxValue = value; }
}
public DataGridViewProgressColumn()
{
CellTemplate = new DataGridViewProgressCell();
((DataGridViewProgressCell)CellTemplate).FillingColor = _fillingColor;
((DataGridViewProgressCell)CellTemplate).MaxValue = _maxValue;
((DataGridViewProgressCell)CellTemplate).DrawText = _drawText;
}
public override sealed DataGridViewCell CellTemplate
{
get { return base.CellTemplate; }
set { base.CellTemplate = value; }
}
}
}
Infine usiamo le classi come nell'esempio in basso:
Occorre creare una colonna di tipo DataGridViewProgressColumn e valorizzarne i dati.
//inizializzazione:
DataGridViewProgressColumn col = new DataGridViewProgressColumn();
col.Name = "Progress";
col.HeaderText = "";
col.Width = 360;
grdDati.Columns.Add(col);
//progress:
DataGridViewProgressCell progressCell = (Row.Cells["Progress"] as DataGridViewProgressCell);
progressCell.MaxValue = 10
for (int i = 1; i <= 10; i++)
{
progressCell.Value = i;
}