Winform Data GridView進捗バーの追加
4417 ワード
この間、リスト(datagridview)を完成して進捗バーを追加し、進捗のリアルタイム表示を実現し、ネット上で資料を調べ、以下の資料を得て応用に成功する必要がある.
public class DataGridViewProgressBarCell : DataGridViewCell
{
public DataGridViewProgressBarCell()
{
this.ValueType = typeof(int);
}
// ;
public DataGridViewProgressBarCell(Color progressBarColor)
: base()
{
ProgressBarColor = progressBarColor;
}
protected Color ProgressBarColor = Color.Green; // , ;
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)
{
using (SolidBrush backBrush = new SolidBrush(cellStyle.BackColor))
{
graphics.FillRectangle(backBrush, cellBounds);
}
base.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
using (SolidBrush brush = new SolidBrush(ProgressBarColor))
{
if (value == null)
value = 0;
int num = (int)value;
float percent = num / 100F;
graphics.FillRectangle(brush, cellBounds.X, cellBounds.Y + 1, cellBounds.Width * percent, cellBounds.Height - 3);
string text = string.Format("{0}%", num);
SizeF rf = graphics.MeasureString(text, cellStyle.Font);
float x = cellBounds.X + (cellBounds.Width - rf.Width) / 2f;
float y = cellBounds.Y + (cellBounds.Height - rf.Height) / 2f;
graphics.DrawString(text, cellStyle.Font, new SolidBrush(cellStyle.ForeColor), x, y);
}
}
}
public class DataGridViewProgressBarColumn : DataGridViewColumn
{
public DataGridViewProgressBarColumn()
: base(new DataGridViewProgressBarCell())
{
CellTemplate = new DataGridViewProgressBarCell();
}
}