2008-6-4 自定义DataGridView列-百分比进度条列
DataGridView是Winform中最常用的控件之一,虽然它功能很强大,但是还是有一些功能没有提供,比如生成百分比的进度。代码如下:
public class DataGridViewProgressBarColumn : DataGridViewColumn
![2008-6-4 自定义DataGridView列-百分比进度条列 2008-6-4 自定义DataGridView列-百分比进度条列](/default/index/img?u=aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzM0Mi8yNGE5MjRhNTdiYTZiM2YyYjUxZmM5ZWRiN2VhNDE4Ni5naWY=)
{
public DataGridViewProgressBarColumn()
: base(new DataGridViewProgressBarCell())
![2008-6-4 自定义DataGridView列-百分比进度条列 2008-6-4 自定义DataGridView列-百分比进度条列](/default/index/img?u=aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzE1Ny83MTVmMmQwNTUwM2I5OWQ0MWYzYjZiYTJjZGNjYzg0ZC5naWY=)
{
}
}
![2008-6-4 自定义DataGridView列-百分比进度条列 2008-6-4 自定义DataGridView列-百分比进度条列](/default/index/img?u=aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzgxNi83MzE2NTU5NTRjN2JlOWQ4ODM1ZWNlNTUxYjUzODVmOC5naWY=)
public class DataGridViewProgressBarCell : DataGridViewCell
![2008-6-4 自定义DataGridView列-百分比进度条列 2008-6-4 自定义DataGridView列-百分比进度条列](/default/index/img?u=aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzM0Mi8yNGE5MjRhNTdiYTZiM2YyYjUxZmM5ZWRiN2VhNDE4Ni5naWY=)
{
public DataGridViewProgressBarCell()
![2008-6-4 自定义DataGridView列-百分比进度条列 2008-6-4 自定义DataGridView列-百分比进度条列](/default/index/img?u=aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzE1Ny83MTVmMmQwNTUwM2I5OWQ0MWYzYjZiYTJjZGNjYzg0ZC5naWY=)
{
![2008-6-4 自定义DataGridView列-百分比进度条列 2008-6-4 自定义DataGridView列-百分比进度条列](/default/index/img?u=aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzczNS8xNDA4YzUyNjBiMmYwNWU0NTBkZWU5MjlkYjliZTVmNy5naWY=)
}
![2008-6-4 自定义DataGridView列-百分比进度条列 2008-6-4 自定义DataGridView列-百分比进度条列](/default/index/img?u=aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzczNS8xNDA4YzUyNjBiMmYwNWU0NTBkZWU5MjlkYjliZTVmNy5naWY=)
//设置进度条的背景色;
public DataGridViewProgressBarCell(Color progressBarColor)
: base()
![2008-6-4 自定义DataGridView列-百分比进度条列 2008-6-4 自定义DataGridView列-百分比进度条列](/default/index/img?u=aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzE1Ny83MTVmMmQwNTUwM2I5OWQ0MWYzYjZiYTJjZGNjYzg0ZC5naWY=)
{
ProgressBarColor = progressBarColor;
}
![2008-6-4 自定义DataGridView列-百分比进度条列 2008-6-4 自定义DataGridView列-百分比进度条列](/default/index/img?u=aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzczNS8xNDA4YzUyNjBiMmYwNWU0NTBkZWU5MjlkYjliZTVmNy5naWY=)
protected Color ProgressBarColor = Color.Green; //进度条的默认背景颜色,绿色;
![2008-6-4 自定义DataGridView列-百分比进度条列 2008-6-4 自定义DataGridView列-百分比进度条列](/default/index/img?u=aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzczNS8xNDA4YzUyNjBiMmYwNWU0NTBkZWU5MjlkYjliZTVmNy5naWY=)
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)
![2008-6-4 自定义DataGridView列-百分比进度条列 2008-6-4 自定义DataGridView列-百分比进度条列](/default/index/img?u=aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzE1Ny83MTVmMmQwNTUwM2I5OWQ0MWYzYjZiYTJjZGNjYzg0ZC5naWY=)
{
using(SolidBrush backBrush = new SolidBrush(cellStyle.BackColor))
![2008-6-4 自定义DataGridView列-百分比进度条列 2008-6-4 自定义DataGridView列-百分比进度条列](/default/index/img?u=aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzE1Ny83MTVmMmQwNTUwM2I5OWQ0MWYzYjZiYTJjZGNjYzg0ZC5naWY=)
{
graphics.FillRectangle(backBrush, cellBounds);
}
base.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
![2008-6-4 自定义DataGridView列-百分比进度条列 2008-6-4 自定义DataGridView列-百分比进度条列](/default/index/img?u=aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzczNS8xNDA4YzUyNjBiMmYwNWU0NTBkZWU5MjlkYjliZTVmNy5naWY=)
using(SolidBrush brush = new SolidBrush(ProgressBarColor))
![2008-6-4 自定义DataGridView列-百分比进度条列 2008-6-4 自定义DataGridView列-百分比进度条列](/default/index/img?u=aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzE1Ny83MTVmMmQwNTUwM2I5OWQ0MWYzYjZiYTJjZGNjYzg0ZC5naWY=)
{
int num = (int) value;
float percent = num / 100F;
graphics.FillRectangle(brush, cellBounds.X, cellBounds.Y+1, cellBounds.Width * percent, cellBounds.Height - 3);
![2008-6-4 自定义DataGridView列-百分比进度条列 2008-6-4 自定义DataGridView列-百分比进度条列](/default/index/img?u=aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzczNS8xNDA4YzUyNjBiMmYwNWU0NTBkZWU5MjlkYjliZTVmNy5naWY=)
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);
}
}
}
Demo运行界面如下图:
Demo运行界面如下图:
转载于:https://www.cnblogs.com/michael-zhangyu/archive/2008/06/04/1213792.html