DataGridView 中添加ProgressBar列

https://www.cnblogs.com/yzeng/p/3725663.html
https://www.cnblogs.com/because/archive/2011/12/18/2292100.html

DataGridView进度条列 C# WinForm

https://www.cnblogs.com/yzeng/p/3725663.html

先看看效果,如果感兴趣,继续往下看……

效果如下图所示:

 

DataGridView里没有Pragress列,但有Image列,有了它我们可以自己绘图来实现进度条。其实实现起来并不困难。

首先在实体类增加Image类型的属性,在get里绘制进度条图片:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;

namespace DataGridViewProgress
{
    public class UserInfo
    {
        public string UserName { get; set; }
        public string Addr { get; set; }
        public int Press { get; set; }

        //进度条图片属性
        public Image PressImg
        {
            get
            {
                Bitmap bmp = new Bitmap(104, 30); //这里给104是为了左边和右边空出2个像素,剩余的100就是百分比的值
                Graphics g = Graphics.FromImage(bmp);
                g.Clear(Color.White); //背景填白色
                //g.FillRectangle(Brushes.Red, 2, 2, this.Press, 26);  //普通效果
                //填充渐变效果
                g.FillRectangle(new LinearGradientBrush(new Point(30, 2), new Point(30, 30), Color.Black, Color.Gray), 2, 2, this.Press, 26);
                return bmp;
            }
        }
    }
}

在这里插入图片描述

然后在DataGridView里添加图片列并绑定DataPropertyName属性:

 

运行起来,大功告成!

DataGridView 中添加ProgressBar列

https://www.cnblogs.com/because/archive/2011/12/18/2292100.html

类是在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 }
复制代码

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章