最近看了看网上关于Winform中给DataGridView控件添加进度条的方法,主要就是利用GDI重绘实现。个人觉得不太美观,因此尝试寻找其他方法,最终算是实现了百分之八十,现在看一下效果:
个人觉得效果还行,现在来说一下实现的思路:
第一步:下载DotNetBar控件并安装
这个就不多说了,DotNetBar在网上的资源很多,随便下一个就行了。安装并破解后如下图所示:
第二步:拖一个DataGridViewX控件到界面上
点击DataGridViewX控件的右上角,选择编辑列,添加两列到表格里。这里要注意了,第一列就是默认类型不用管,第二列因为要显示进度条,所以ColumnType选择DataGridViewProgressBarXColumn,如下图所示:
完成后的效果如下图所示:
第三步:控制进度条的颜色变化
从第一张图中可以看出,进度条有红、黄、蓝三种颜色,要实现这种效果需要对进度条列的BeforeCellPaint事件进行重写,在这个Demo中,进度条列是的列名是columnProgressBar,代码如下:
private void InitializeDataGridViewX()
{
DataGridViewProgressBarXColumn column = dgv.Columns["columnProgressBar"] as DataGridViewProgressBarXColumn;
column.BeforeCellPaint += (object sender, BeforeCellPaintEventArgs e) =>
{
if (column.Value < 30)
{
column.ColorTable = eProgressBarItemColor.Error;
}
else if (column.Value >= 30 && column.Value < 70)
{
column.ColorTable = eProgressBarItemColor.Paused;
}
else
{
column.ColorTable = eProgressBarItemColor.Normal;
}
};
}
第四步:添加一个AsyncHelper类
我这里模拟一个读取txt文件内容的过程,还是利用BackGroundWorker组件实现,如果不太了解BackGroundWorker组件,可以看一看我之前的博客,这里就不赘述了,代码如下:
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public class AsyncHelper
{
///
/// 文本文件路径
///
private string filePath;
public string[] fileContent { get { return File.ReadAllLines(filePath); } }
///
/// BackgroundWorker组件
///
public BackgroundWorker Bg_Worker { get; set; }
///
/// 表格内的进度条
///
private DataGridViewProgressBarXCell progressBarCell;
///
/// 构造函数
///
///
public AsyncHelper(string filePath, DataGridViewProgressBarXCell progressBarCell)
{
this.filePath = filePath;
this.progressBarCell = progressBarCell;
// 创建组件
this.Bg_Worker = new BackgroundWorker();
this.Bg_Worker.WorkerReportsProgress = true;
this.Bg_Worker.WorkerSupportsCancellation = true;
// 绑定事件
this.Bg_Worker.DoWork += bg_DoWork;
this.Bg_Worker.ProgressChanged += bg_ProgressChanged;
this.Bg_Worker.RunWorkerCompleted += bg_RunWorkerCompleted;
}
///
/// DoWork事件
///
///
///
private void bg_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 1; i <= fileContent.Length; i++)
{
if (Bg_Worker.CancellationPending)
{
e.Cancel = true;
break;
}
else
{
System.Threading.Thread.Sleep(150);
string text = fileContent[i - 1];
}
Bg_Worker.ReportProgress(i);
}
}
///
/// ProgressChanged事件
///
///
///
private void bg_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBarCell.Value = (int)(e.ProgressPercentage * 1.0 / fileContent.Length * 100);
}
///
/// RunWorkerCompleted事件
///
///
///
private void bg_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show("转换发生异常!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (e.Cancelled)
{
MessageBox.Show("转换终止!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
///
/// 执行任务
///
public void Do()
{
Bg_Worker.RunWorkerAsync();
}
}
}
这个类是对单个txt文件进行读取。这里需要注意一点,进度条列的最大值都是100,但是每个txt文件的行数不一定都是100,因此进度条的数值应该利用百分比表示。
第五步:完善主界面代码
主界面的代码很简单,如下所示:
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class MainForm : DevComponents.DotNetBar.RibbonForm
{
///
/// 构造函数
///
public MainForm()
{
InitializeComponent();
InitializeDataGridViewX();
}
///
/// 初始化DataGridViewX
///
private void InitializeDataGridViewX()
{
DataGridViewProgressBarXColumn column = dgv.Columns["columnProgressBar"] as DataGridViewProgressBarXColumn;
column.BeforeCellPaint += (object sender, BeforeCellPaintEventArgs e) =>
{
if (column.Value < 30)
{
column.ColorTable = eProgressBarItemColor.Error;
}
else if (column.Value >= 30 && column.Value < 70)
{
column.ColorTable = eProgressBarItemColor.Paused;
}
else
{
column.ColorTable = eProgressBarItemColor.Normal;
}
};
}
///
/// 添加文件
///
///
///
private void btnAddFile_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "添加文件";
openFileDialog.Filter = "文本文件(*.txt)|*.txt";
openFileDialog.Multiselect = true;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
foreach (string filePath in openFileDialog.FileNames)
{
int index = dgv.Rows.Add();
dgv.Rows[index].Cells[0].Value = filePath;
}
}
}
///
/// 读取文件
///
///
///
private void btnReadFile_Click(object sender, EventArgs e)
{
for (int i = 0; i < dgv.Rows.Count - 1; i++)
{
string filePath = dgv.Rows[i].Cells[0].Value.ToString();
DataGridViewProgressBarXCell progressBarCell = dgv.Rows[i].Cells[1] as DataGridViewProgressBarXCell;
// 开始读取
AsyncHelper helper = new AsyncHelper(filePath, progressBarCell);
helper.Do();
}
}
}
}
至此完成,本方法相对于GDI重绘的方法更加简单直观。本次利用VS2015进行开发,Demo的下载地址如下:
链接:https://pan.baidu.com/s/1oG152m_QrmVfKXgRVQi-wQ
提取码:trr2