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 }
複製代碼

 

 

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