using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
namespace EFwinformTools.appclass
{
class DataGridViewProgressColumn : DataGridViewImageColumn
{
public DataGridViewProgressColumn()
{
this.CellTemplate = new DataGridViewProgressCell();
}
}
class DataGridViewProgressCell : DataGridViewImageCell
{
static Image emptyImage;
static DataGridViewProgressCell()
{
emptyImage = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
}
public DataGridViewProgressCell()
{
this.ValueType = typeof(int);
}
public string ShowText { get; set; } //如果要显示独立的文字而不是百分比,设置此属性。
protected override object GetFormattedValue(object value,
int rowIndex, ref DataGridViewCellStyle cellStyle,
TypeConverter valueTypeConverter,
TypeConverter formattedValueTypeConverter,
DataGridViewDataErrorContexts context)
{
return emptyImage;
}
protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
string tValue = value.ToString();
if (tValue == "") tValue = "0";
int progressVal;
try { progressVal = Convert.ToInt16(tValue); }
catch
{
progressVal = 0;
}
float percentage = ((float)progressVal / 100.0f);
Brush backColorBrush = new SolidBrush(cellStyle.BackColor);
Brush foreColorBrush = new SolidBrush(cellStyle.ForeColor);
base.Paint(g, clipBounds, cellBounds,
rowIndex, cellState, value, formattedValue, errorText,
cellStyle, advancedBorderStyle, (paintParts & ~DataGridViewPaintParts.ContentForeground));
string DrawStringStr = progressVal.ToString() + "%";
if (ShowText != "")
{
DrawStringStr = ShowText;
}
if (percentage > 0.0)
{
g.FillRectangle(new SolidBrush(Color.FromArgb(163, 189, 242)), cellBounds.X + 2, cellBounds.Y + 2, Convert.ToInt32((percentage * cellBounds.Width - 4)), cellBounds.Height - 4);
g.DrawString(DrawStringStr, cellStyle.Font, foreColorBrush, cellBounds.X + 30, cellBounds.Y + 5);
}
else
{
if (this.DataGridView.CurrentRow.Index == rowIndex)
g.DrawString(DrawStringStr, cellStyle.Font, new SolidBrush(cellStyle.SelectionForeColor), cellBounds.X + 30, cellBounds.Y + 5);
else
g.DrawString(DrawStringStr, cellStyle.Font, foreColorBrush, cellBounds.X + 30, cellBounds.Y + 5);
}
}
}
}
ShowText属性,是为了把显示文字可以独立设置。默认是显示百分比。
使用时,直接绑定含有百分比的整型数字即可,绑定时无法独立设置显示的值,下面的方法是独立设置值时的方法
DataGridViewProgressCell ProgressCell= new DataGridViewProgressCell();
string fValue = PatentList.Rows[i]["Correlation"].ToString();
if (fValue == "") fValue = "0";
try
{
ProgressCell.Value = Convert.ToInt16(fValue) * 10;
ProgressCell.ShowText = fValue.ToString();
}
catch
{
ProgressCell.Value = 0;
ProgressCell.ShowText = "";
}
// PatentRow.Cells[3] = ProgressCell; //指定单元格为上面定义的ProgressCell
值变化重绘时:
foreach (DataGridViewRow SelectDr in dataGridView2.SelectedRows)
{
SelectDr.Cells["Progress"].Value = value;
((DataGridViewProgressCell)SelectDr.Cells[3]).ShowText = value.ToString();
}
上图: