---
title: "DataGridView增加进度条列"
author: "dzgwt2004"
date: "2012-03-12T19:24:52Z"
tags: []
categories: ["编程语言"]
slug: "datagridview"
draft: false
lastmod: "2012-03-12T19:27:45Z"
id: 1451069
url: "https://digga2012.iteye.com/blog/1451069"
---

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 &gt; 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例(附光盘第Ⅱ卷)(王小科//王军)(清华大学出版社)](http://img01.taobaocdn.com/bao/uploaded/i5/T1Aid3XehCXXcPJAZ._082905.jpg_310x310.jpg)](http://s.click.taobao.com/t_8?e=7HZ6jHSTbIQxaaaKQZJvpijxZKOFbxdRzHkz9Nunk%2BScfw%3D%3D&p=mm_30417831_0_0 "ASP.NET从入门到精通")