DataGridView增加进度条列

1.定义进度条列
class DataGridViewProgressColumn : DataGridViewImageColumn
{

public DataGridViewProgressColumn()
{
this.CellTemplate = new DataGridViewProgressCell();

}
}

class DataGridViewProgressCell : DataGridViewImageCell
{
//public UpdateProcessValue updateValue;

public object progressCellLock = new object();

static Image emptyImage;
static DataGridViewProgressCell()
{
emptyImage = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
}

public DataGridViewProgressCell()
{
this.ValueType = typeof(double);
}

public string ShowText { get; set; } //如果要显示独立的文字而不是百分比,设置此属性。
protected override object GetFormattedValue(object value,
int rowIndex, ref DataGridViewCellStyle cellStyle,
TypeConverter valueTypeConverter,
TypeConverter formattedValueTypeConverter,
DataGridViewDataErrorContexts context)
{
return emptyImage;
}

public new double Value
{
set
{
lock (progressCellLock)
{
try
{
base.Value = Math.Round(value, 2);
}
catch { }
}
}
get
{
return double.Parse(base.Value.ToString());
}
}



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)
{
if (value == null)
{
value = "0";
ShowText = "0%";
}
string tValue = value.ToString();
if (tValue == "") tValue = "0";


double progressVal;
try { progressVal = Convert.ToDouble(tValue); }
catch
{
progressVal = 0;
}

float percentage = ((float)progressVal / 100.0f);
Brush backColorBrush = new SolidBrush(cellStyle.BackColor);
Brush foreColorBrush = new SolidBrush(cellStyle.ForeColor);

lock (progressCellLock)
{
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.FillRectangle(new SolidBrush(Color.Green), 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);
}
}
}
}

 

2.使用
Progress = new DataGridViewProgressColumn();

 C#开发实战1200例(附光盘第Ⅱ卷)(王小科//王军)(清华大学出版社)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值