效果图
先上一张效果图
实现思路
1、先设计一个进度条类型的DataGridViewColumn。
2、然后在DataGridView控件中使用。
关键代码
1、DataGridViewProgressBarCellStyle.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Mesnac.Controls.Default.ProgressBarGridView
{
public class DataGridViewProgressBarCellStyle : DataGridViewCellStyle
{
#region 定义字段
private Color _progressBarColor = Color.Green; //进度条的默认背景颜色,绿色;
private bool _isShowProgressText = true; //进度条是否显示百分比文本,默认显示
#endregion
#region 构造方法
public DataGridViewProgressBarCellStyle() : base()
{
}
public DataGridViewProgressBarCellStyle(DataGridViewProgressBarCellStyle dgvcs) : base()
{
}
public DataGridViewProgressBarCellStyle(Color progressBarColor, bool isShowProgressText, DataGridViewCellStyle dgvcs) : base(dgvcs)
{
this._progressBarColor = progressBarColor;
this._isShowProgressText = isShowProgressText;
}
#endregion
#region 属性定义
public Color ProgressBarColor
{
get { return _progressBarColor; }
set { _progressBarColor = value; }
}
public bool IsShowProgressText
{
get { return _isShowProgressText; }
set { _isShowProgressText = value; }
}
#endregion
}
}
2、DataGridViewProgressBarCell.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Mesnac.Controls.Default.ProgressBarGridView
{
/// <summary>
/// 进度条单元格类定义
/// </summary>
public class DataGridViewProgressBarCell : DataGridViewCell
{
#region 定义字段
private Color _progressBarColor = Color.Green; //进度条的默认背景颜色,绿色;
private bool _isShowProgressText = true; //进度条是否显示百分比文本,默认显示
#endregion
#region 构造方法
/// <summary>
/// 默认构造方法
/// </summary>
public DataGridViewProgressBarCell()
{
this.ValueType = typeof(int);
}
/// <summary>
/// 设置进度条的背景色;
/// </summary>
/// <param name="progressBarColor">进度条的背景色</param>
public DataGridViewProgressBarCell(Color progressBarColor) : base()
{
this._progressBarColor = progressBarColor;
}
/// <summary>
/// 设置是否显示进度百分比文本
/// </summary>
/// <param name="isShowProgressText">是否显示进度百分比文本</param>
public DataGridViewProgressBarCell(bool isShowProgressText) : base()
{
this._isShowProgressText = isShowProgressText;
}
/// <summary>
/// 设置进度条的背景色和是否显示进度百分比文本
/// </summary>
/// <param name="progressBarColor">进度条的背景色</param>
/// <param name="isShowProgressText">是否显示进度百分比文本</param>
public DataGridViewProgressBarCell(Color progressBarColor, bool isShowProgressText) : base()
{
this._progressBarColor = progressBarColor;
this._isShowProgressText = isShowProgressText;
}
#endregion
public bool IsShowProgressText
{
set
{
this._isShowProgressText = value;
}
}
#region 重写绘制方法
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
try
{
Console.WriteLine("Paint");
DataGridViewProgressBarCellStyle dgvcs = cellStyle as DataGridViewProgressBarCellStyle;
if (dgvcs != null)
{
this._progressBarColor = dgvcs.ProgressBarColor;
this._isShowProgressText = dgvcs.IsShowProgressText;
Console.WriteLine(dgvcs.ProgressBarColor.ToString());
Console.WriteLine(dgvcs.IsShowProgressText);
}
using (SolidBrush backBrush = new SolidBrush(cellStyle.BackColor))
{
graphics.FillRectangle(backBrush, cellBounds);
}
base.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
using (SolidBrush brush = new SolidBrush(_progressBarColor))
{
if (value == null || value == System.DBNull.Value)
value = 0;
int num = (int)value;
float percent = num / 100F;
graphics.FillRectangle(brush, cellBounds.X, cellBounds.Y + 1, cellBounds.Width * percent, cellBounds.Height - 3);
if (this._isShowProgressText)
{
string text = string.Format("{0}%", num);
SizeF rf = graphics.MeasureString(text, cellStyle.Font);
float x = cellBounds.X + (cellBounds.Width - rf.Width) / 2f;
float y = cellBounds.Y + (cellBounds.Height - rf.Height) / 2f;
graphics.DrawString(text, cellStyle.Font, new SolidBrush(cellStyle.ForeColor), x, y);
}
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
#endregion
}
}
3、DataGridViewProgressBarColumn.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Mesnac.Controls.Default.ProgressBarGridView
{
/// <summary>
/// 定义进度条列实现类
/// </summary>
public class DataGridViewProgressBarColumn: DataGridViewColumn
{
#region 构造方法
/// <summary>
/// 默认构造方法
/// </summary>
public DataGridViewProgressBarColumn() : base(new DataGridViewProgressBarCell())
{
CellTemplate = new DataGridViewProgressBarCell();
}
#endregion
}
}
4、使用代码
using Mesnac.Controls.Default.ProgressBarGridView;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.dataGridView1.DataError += dataGridView1_DataError;
this.InitColumns();
this.InitData();
}
void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
Console.WriteLine(e.Exception.Message);
}
private void InitColumns()
{
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
DataGridViewTextBoxColumn colId = new DataGridViewTextBoxColumn();
colId.Name = "colId";
colId.HeaderText = "Id";
colId.DataPropertyName = "Id";
DataGridViewTextBoxColumn colName = new DataGridViewTextBoxColumn();
colName.Name = "colName";
colName.HeaderText = "Name";
colName.DataPropertyName = "Name";
DataGridViewProgressBarColumn colValue = new DataGridViewProgressBarColumn();
colValue.Name = "colValue";
colValue.HeaderText = "Value";
colValue.DataPropertyName = "Value";
colValue.DefaultCellStyle.ForeColor = Color.Blue;
(colValue.CellTemplate as DataGridViewProgressBarCell).IsShowProgressText = false;
this.dataGridView1.Columns.Clear();
this.dataGridView1.Columns.Add(colId);
this.dataGridView1.Columns.Add(colName);
this.dataGridView1.Columns.Add(colValue);
this.dataGridView1.CellFormatting += dataGridView1_CellFormatting;
}
void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridView dgv = sender as DataGridView;
if (dgv != null && dgv.Columns[e.ColumnIndex].GetType() == typeof(DataGridViewProgressBarColumn))
{
Console.WriteLine(e.Value);
DataGridViewProgressBarCellStyle dgvcs = new DataGridViewProgressBarCellStyle(Color.YellowGreen, true, e.CellStyle);
e.CellStyle = dgvcs;
}
}
private void InitData()
{
DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Value", typeof(int));
DataRow r1 = dt.NewRow();
r1["Id"] = 1;
r1["Name"] = "Tom";
r1["Value"] = 30;
DataRow r2 = dt.NewRow();
r2["Id"] = 2;
r2["Name"] = "Berry";
r2["Value"] = 50;
DataRow r3 = dt.NewRow();
r3["Id"] = 3;
r3["Name"] = "Marry";
r3["value"] = 98;
dt.Rows.Add(r1);
dt.Rows.Add(r2);
dt.Rows.Add(r3);
this.dataGridView1.DataSource = dt;
}
}
}
以下实例中还包括实现以进度百分比显示的进度条控件,如下图