当前位置:网站首页>C#实现给DevExpress中GridView表格指定列添加进度条显示效果——代码实现方式
C#实现给DevExpress中GridView表格指定列添加进度条显示效果——代码实现方式
2022-06-26 07:01:00 【牛奶咖啡13】
一、问题描述
在我们使用Winform配合DevExpress进行开发表格时,表格中的涉及到百分比数据的内容除了显示百分比的数字内容外,还希望搭配显示进度条效果(且低于百分之60的内容用红色表示不合格,高于百分之60的用绿色表示),这样百分比的显示效果更加清晰直观。

二、实现方法
2.1、先注册单元格绘制方法

2.2、编写给指定单元格绘制进度条的方法
#region 给表格指定列绘制进度条
/// <summary>
/// 给指定列绘制进度条
/// </summary>
/// <param name="gridView">GridView控件</param>
/// <param name="columnFieldName">需绘制进度条列的字段名称</param>
/// <param name="warningValue">警告值(用于区分不同的颜色)</param>
/// <param name="beforeWaringValueColor">警告值前的进度条颜色</param>
/// <param name="afterWaringValueColor">警告值后的进度条颜色</param>
public static void DrawProgressBarToColumn(DevExpress.XtraGrid.Views.Grid.GridView gridView, string columnFieldName,
double warningValue = 60, Brush beforeWaringValueColor = null, Brush afterWaringValueColor = null)
{
var column = gridView.Columns[columnFieldName];
if (column == null) return;
column.AppearanceCell.Options.UseTextOptions = true;
//设置该列显示文本内容的位置(这里默认居中)
column.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
//绘制事件方法(前提需要先注册才能够接收到参数使用)
gridView.CustomDrawCell += (s, e) =>
{
if (e.Column.FieldName == columnFieldName)
{
DrawProgressBar(e, warningValue, beforeWaringValueColor, afterWaringValueColor);
e.Handled = true;
DrawEditor(e);
}
};
}
/// <summary>
/// 绘制进度条
/// </summary>
/// <param name="e">单元格绘制事件参数</param>
/// <param name="warningValue">警告值(用于区分不同的颜色)</param>
/// <param name="beforeWaringValueColor">警告值前的进度条颜色</param>
/// <param name="afterWaringValueColor">警告值后的进度条颜色</param>
public static void DrawProgressBar(DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e, double warningValue = 60,
Brush beforeWaringValueColor = null, Brush afterWaringValueColor = null)
{
string tmpValue = e.CellValue.ToString();
float percent = 0;
if (!string.IsNullOrEmpty(tmpValue))
{
float.TryParse(tmpValue, out percent);
}
int width = 0;
if (percent >2)
{
width = (int)(Math.Abs(percent / 100) * e.Bounds.Width);
}
else
{
width = (int)(Math.Abs(percent) * e.Bounds.Width);
}
Rectangle rect = new Rectangle(e.Bounds.X, e.Bounds.Y, width, e.Bounds.Height);
Brush b = Brushes.Green;
if (afterWaringValueColor != null)
{
b = afterWaringValueColor;
}
if (percent < warningValue)
{
if (beforeWaringValueColor == null)
{
b = Brushes.Red;
}
else
{
b = beforeWaringValueColor;
}
}
e.Graphics.FillRectangle(b, rect);
}
/// <summary>
/// 绘制单元格
/// </summary>
/// <param name="e">单元格绘制事件参数</param>
public static void DrawEditor(DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
{
DevExpress.XtraGrid.Views.Grid.ViewInfo.GridCellInfo cell = e.Cell as DevExpress.XtraGrid.Views.Grid.ViewInfo.GridCellInfo;
Point offset = cell.CellValueRect.Location;
DevExpress.XtraEditors.Drawing.BaseEditPainter pb = cell.ViewInfo.Painter as DevExpress.XtraEditors.Drawing.BaseEditPainter;
AppearanceObject style = cell.ViewInfo.PaintAppearance;
if (!offset.IsEmpty)
cell.ViewInfo.Offset(offset.X, offset.Y);
try
{
pb.Draw(new DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs(cell.ViewInfo, e.Cache, cell.Bounds));
}
finally
{
if (!offset.IsEmpty)
{
cell.ViewInfo.Offset(-offset.X, -offset.Y);
}
}
}
#endregion 2.3、使用给指定单元格绘制进度条方法
注意:在使用给指定单元格绘制进度条方法时(如果数字都是小于1的那么警告值也是需要小于1;如果是大于1的则按照需要设置即可)。
--使用方法语法
DrawProgressBarToColumn(gridView组件名称, 需要绘制进度条的单元格字段, 警告值,警告值前的进度条颜色,警告值后的进度条颜色);
--示例1
DrawProgressBarToColumn(gridView1, "Age", 0.6,Brushes.OrangeRed,Brushes.LawnGreen);
--示例2
DrawProgressBarToColumn(gridView1, "Age", 0.6, new SolidBrush(Color.FromArgb(236, 65, 65)), new SolidBrush(Color.FromArgb(45, 115, 186)));三、相关内容
3.1、给单元格设置百分比
/// <summary>
/// 设置表格指定单元格都使用百分比
/// </summary>
/// <param name="gridView">gridView组件</param>
/// <param name="columnName">列名称</param>
public static void SetPercentage(GridView gridView, string columnName)
{
if (gridView != null && gridView.Columns.Count > 0 && !string.IsNullOrEmpty(columnName))
{
gridView.Columns[columnName].DisplayFormat.FormatType = FormatType.Numeric;
gridView.Columns[columnName].DisplayFormat.FormatString = "P2";
}
}上面这个代码实现的是将小数转为百分比显示【比如将小数(0.3)使用后就转为(30%)显示】
/// <summary>
/// 给表格指定单元格都保留2位小数后添加%号
/// </summary>
/// <param name="gridView"></param>
/// <param name="startColumnIndex"></param>
/// <param name="endColumnIndex"></param>
public static void SetResreserveTwoBitAndPercentageChar(GridView gridView, int startColumnIndex, int endColumnIndex)
{
if (gridView != null && gridView.Columns.Count > 0 &&
startColumnIndex > 0 && endColumnIndex > 0 &&
endColumnIndex <= gridView.Columns.Count)
{
for (int i = startColumnIndex; i <= endColumnIndex; i++)
{
gridView.Columns[i].DisplayFormat.FormatType = FormatType.Numeric;
gridView.Columns[i].DisplayFormat.FormatString = $"{0:N2}'%'";
}
}
}上面这个代码实现的是给数字添加百分号符号显示【比如将小数(86.2356)使用后就转为(86.24%)显示】
3.2、相关资料
RowCellCustomDrawEventArgs Class | WinForms Controls | DevExpress Documentation
https://docs.devexpress.com/WindowsForms/DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgsBrush 类 (System.Drawing) | Microsoft Docs
https://docs.microsoft.com/zh-cn/dotnet/api/system.drawing.brush?view=dotnet-plat-ext-6.0Graphics.DrawEllipse 方法 (System.Drawing) | Microsoft Docs
https://docs.microsoft.com/zh-cn/dotnet/api/system.drawing.graphics.drawellipse?view=dotnet-plat-ext-6.0SolidBrush 类 (System.Drawing) | Microsoft Docs
https://docs.microsoft.com/zh-cn/dotnet/api/system.drawing.solidbrush?view=dotnet-plat-ext-6.0
边栏推荐
- 数据挖掘是什么?
- Numpy learning challenge level 4 - numpy array attribute
- NumPy学习挑战第一关-NumPy的下载与安装
- 【图像分割】基于最大主曲率实现视网膜眼底图像中的血管提取附matlab代码
- [cellular automata] Based on cellular automata, realize the traffic flow problem of expressway toll station, with matlab code
- NumPy学习挑战第五关-创建数组
- Deep exploration image theme color extraction
- Screen sharing recommendations
- Shell input validation alphanumeric only
- Guide to "avoid dismissal during probation period"
猜你喜欢
![[image segmentation] blood vessel extraction from retinal fundus images based on maximum principal curvature with matlab code](/img/10/3cef095ae5834bbbe51ef16b5c9f78.png)
[image segmentation] blood vessel extraction from retinal fundus images based on maximum principal curvature with matlab code

Procedure macros in rust

Installation and login of MySQL database

LabVIEW arduino TCP / IP Remote Intelligent Home System (Project section - 5)

I use flask to write the website "II"

数据挖掘是什么?

Solution of garbled code in sparkshell deletion key of SecureCRT

报错问题Parameter index out of range(0 < 1) (1 > number of parameters,which is 0

Invalid problem of self defined map used by Gaode map

Paths with a certain value in a binary tree (1) (2) (3) (Sword finger offer)
随机推荐
Shell programming - user information management
China's audio industry competition trend outlook and future development trend forecast report 2022 Edition
How to transfer database data to check box
Paths with a certain value in a binary tree (1) (2) (3) (Sword finger offer)
MATLAB线性规划模型学习笔记
Closure problem C Lua
Bugku exercise ---misc--- prosperity, strength and democracy
Solve the problem of cross domain invalidation of cookies in the new version of Google Chrome browser
Spark3.3.0源码编译补充篇-抓狂的证书问题
报错问题Parameter index out of range(0 < 1) (1 > number of parameters,which is 0
Professional course - Code question record
Research Report on market supply and demand and strategy of China's pallet scale industry
My SQL (II)
Research Report on market development prospect and investment strategy of China's water soluble film industry 2022-2027
解决新版谷歌Chrome浏览器Cookie跨域失效问题
NumPy学习挑战第五关-创建数组
MySQL operation database
[image fusion] MRI-CT image fusion based on gradient energy, local energy and PCA fusion rules with matlab code
网络io,磁盘io
Zraqnhydae