DataGridView 中添加ProgressBar列

类是在msdn.microsoft上找到的

http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/769ca9d6-1e9d-4d76-8c23-db535b2f19c2/

类代码如下:

//---------------------------------------------------------------------
//THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//PARTICULAR PURPOSE.
//---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
namespace DataGridViewProgress
{
public class DataGridViewProgressColumn : DataGridViewImageColumn
{
public DataGridViewProgressColumn()
{
CellTemplate = new DataGridViewProgressCell();
}
}
}
namespace DataGridViewProgress
{
class DataGridViewProgressCell : DataGridViewImageCell
{
// Used to make custom cell consistent with a DataGridViewImageCell
static Image emptyImage;
static DataGridViewProgressCell()
{
emptyImage = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
}
public DataGridViewProgressCell()
{
this.ValueType = typeof(int);
}
// Method required to make the Progress Cell consistent with the default Image Cell.
// The default Image Cell assumes an Image as a value, although the value of the Progress Cell is an int.
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)
{
int progressVal;
if (value != null)
progressVal = (int)value;
else
progressVal = 1;


float percentage = ((float)progressVal / 100.0f); // Need to convert to float before division; otherwise C# returns int which is 0 for anything but 100%.
Brush backColorBrush = new SolidBrush(cellStyle.BackColor);
Brush foreColorBrush = new SolidBrush(cellStyle.ForeColor);
// Draws the cell grid
base.Paint(g, clipBounds, cellBounds,
rowIndex, cellState, value, formattedValue, errorText,
cellStyle, advancedBorderStyle, (paintParts & ~DataGridViewPaintParts.ContentForeground));
if (percentage > 0.0)
{
// Draw the progress bar and the text
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(progressVal.ToString() + "%", cellStyle.Font, foreColorBrush, cellBounds.X + 6, cellBounds.Y + 2);
}
else
{
// draw the text
if (this.DataGridView.CurrentRow.Index == rowIndex)
g.DrawString(progressVal.ToString() + "%", cellStyle.Font, new SolidBrush(cellStyle.SelectionForeColor), cellBounds.X + 6, cellBounds.Y + 2);
else
g.DrawString(progressVal.ToString() + "%", cellStyle.Font, foreColorBrush, cellBounds.X + 6, cellBounds.Y + 2);
}
}
}
}

 

使用方法:

添加DataGridView控件在Form上,将上述类添加到工程中,直接编译,编译后在DataGridView的列编辑模式中可以直接选择ColumnType的DataGridViewProgressColumn,该列就变为了ProgressColumn类型了。

后续直接设置需要控制进度条的值即可控制进度条的显示。 

 

 

工程文件:

点此下载

 

 


 


20120107更新

上述代码在

if (percentage > 0.0)

这行有点bug,由于percentage is a float,这里按照逻辑应该是

if (percentage > 0.0 || percentage-0.0<float.Epsilon)

其实就是percentage>=0;

全部代码如下:

 1 //---------------------------------------------------------------------
2 //THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
3 //KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
4 //IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
5 //PARTICULAR PURPOSE.
6 //---------------------------------------------------------------------
7 namespace DataGridViewProgress
8 {
9 public class DataGridViewProgressColumn : DataGridViewImageColumn
10 {
11 public DataGridViewProgressColumn()
12 {
13 CellTemplate = new DataGridViewProgressCell();
14 }
15 }
16 }
17 namespace DataGridViewProgress
18 {
19 class DataGridViewProgressCell : DataGridViewImageCell
20 {
21 // Used to make custom cell consistent with a DataGridViewImageCell
22 static Image emptyImage;
23 static DataGridViewProgressCell()
24 {
25 emptyImage = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
26 }
27 public DataGridViewProgressCell()
28 {
29 this.ValueType = typeof(int);
30 }
31 // Method required to make the Progress Cell consistent with the default Image Cell.
32 // The default Image Cell assumes an Image as a value, although the value of the Progress Cell is an int.
33 protected override object GetFormattedValue(object value,
34 int rowIndex, ref DataGridViewCellStyle cellStyle,
35 TypeConverter valueTypeConverter,
36 TypeConverter formattedValueTypeConverter,
37 DataGridViewDataErrorContexts context)
38 {
39 return emptyImage;
40 }
41
42 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)
43 {
44 int progressVal;
45 if (value != null)
46 progressVal = (int)value;
47 else
48 progressVal = 0;
49
50
51 float percentage = ((float)progressVal / 100.0f); // Need to convert to float before division; otherwise C# returns int which is 0 for anything but 100%.
52 Brush backColorBrush = new SolidBrush(cellStyle.BackColor);
53 Brush foreColorBrush = new SolidBrush(cellStyle.ForeColor);
54 // Draws the cell grid
55 base.Paint(g, clipBounds, cellBounds,
56 rowIndex, cellState, value, formattedValue, errorText,
57 cellStyle, advancedBorderStyle, (paintParts & ~DataGridViewPaintParts.ContentForeground));
58 if (percentage > 0.0 || percentage-0.0<float.Epsilon)
59 {
60 // Draw the progress bar and the text
61 g.FillRectangle(new SolidBrush(Color.FromArgb(163, 189, 242)), cellBounds.X + 2, cellBounds.Y + 2, Convert.ToInt32((percentage * cellBounds.Width - 4)), cellBounds.Height - 4);
62 g.DrawString(progressVal.ToString() + "%", cellStyle.Font, foreColorBrush, cellBounds.X + 6, cellBounds.Y + 2);
63 }
64 else
65 {
66 // draw the text
67 if (this.DataGridView.CurrentRow.Index == rowIndex)
68 g.DrawString(progressVal.ToString() + "%", cellStyle.Font, new SolidBrush(cellStyle.SelectionForeColor), cellBounds.X + 6, cellBounds.Y + 2);
69 else
70 g.DrawString(progressVal.ToString() + "%", cellStyle.Font, foreColorBrush, cellBounds.X + 6, cellBounds.Y + 2);
71 }
72 }
73 }
74 }

 

 

posted @ 2011-12-18 21:38  璇星  阅读(5769)  评论(1编辑  收藏  举报