当前位置:网站首页>C implementation adds a progress bar display effect to the specified column of the GridView table in devaxpress - code implementation method
C implementation adds a progress bar display effect to the specified column of the GridView table in devaxpress - code implementation method
2022-06-26 07:09:00 【Milk coffee 13】
One 、 Problem description
When we use Winform coordination DevExpress When developing tables , The contents related to percentage data in the table except for the numerical contents showing the percentage , I also hope it can be combined with the progress bar display effect ( And less than percent 60 The contents of are indicated as unqualified in red , More than percent 60 In green ), In this way, the percentage display effect is more clear and intuitive .

Two 、 Implementation method
2.1、 First register the cell drawing method

2.2、 Write a method to draw a progress bar to a specified cell
#region Draw a progress bar for the specified column of the table
/// <summary>
/// Draw a progress bar for the specified column
/// </summary>
/// <param name="gridView">GridView Control </param>
/// <param name="columnFieldName"> Name of the field to draw the progress bar column </param>
/// <param name="warningValue"> Warning value ( Used to distinguish different colors )</param>
/// <param name="beforeWaringValueColor"> Progress bar color before warning value </param>
/// <param name="afterWaringValueColor"> Progress bar color after warning value </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;
// Set the position where the text content is displayed in this column ( The default here is centered )
column.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
// Draw event methods ( Before receiving parameter usage, you need to register first )
gridView.CustomDrawCell += (s, e) =>
{
if (e.Column.FieldName == columnFieldName)
{
DrawProgressBar(e, warningValue, beforeWaringValueColor, afterWaringValueColor);
e.Handled = true;
DrawEditor(e);
}
};
}
/// <summary>
/// Draw a progress bar
/// </summary>
/// <param name="e"> Cell draw event parameters </param>
/// <param name="warningValue"> Warning value ( Used to distinguish different colors )</param>
/// <param name="beforeWaringValueColor"> Progress bar color before warning value </param>
/// <param name="afterWaringValueColor"> Progress bar color after warning value </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>
/// Draw cells
/// </summary>
/// <param name="e"> Cell draw event parameters </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、 Use the draw progress bar method for the specified cell
Be careful : When using the draw progress bar method for the specified cell ( If the numbers are all less than 1 The warning value also needs to be less than 1; If it is greater than 1 And set as required ).
-- Use method syntax
DrawProgressBarToColumn(gridView Component name , You need to draw the cell field of the progress bar , Warning value , Progress bar color before warning value , Progress bar color after warning value );
-- Example 1
DrawProgressBarToColumn(gridView1, "Age", 0.6,Brushes.OrangeRed,Brushes.LawnGreen);
-- Example 2
DrawProgressBarToColumn(gridView1, "Age", 0.6, new SolidBrush(Color.FromArgb(236, 65, 65)), new SolidBrush(Color.FromArgb(45, 115, 186)));3、 ... and 、 Related content
3.1、 Set the percentage for the cell
/// <summary>
/// Set the table to use percentages for all specified cells
/// </summary>
/// <param name="gridView">gridView Components </param>
/// <param name="columnName"> Column name </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";
}
}The above code is to convert decimal to percentage display 【 For example, the decimal number (0.3) After use, it will be changed to (30%) Show 】
/// <summary>
/// All cells assigned to the table are reserved 2 Add... After decimal places % Number
/// </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}'%'";
}
}
}The above code is to add a percent sign to the number to display 【 For example, the decimal number (86.2356) After use, it will be changed to (86.24%) Show 】
3.2、 Related information
RowCellCustomDrawEventArgs Class | WinForms Controls | DevExpress Documentation
https://docs.devexpress.com/WindowsForms/DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgsBrush class (System.Drawing) | Microsoft Docs
https://docs.microsoft.com/zh-cn/dotnet/api/system.drawing.brush?view=dotnet-plat-ext-6.0Graphics.DrawEllipse Method (System.Drawing) | Microsoft Docs
https://docs.microsoft.com/zh-cn/dotnet/api/system.drawing.graphics.drawellipse?view=dotnet-plat-ext-6.0SolidBrush class (System.Drawing) | Microsoft Docs
https://docs.microsoft.com/zh-cn/dotnet/api/system.drawing.solidbrush?view=dotnet-plat-ext-6.0
边栏推荐
- SQL Basics
- China polyimide film market demand and future investment risk outlook report 2022-2027
- Scratch program learning
- How to choose securities companies for stock speculation? Is it safe to open a mobile account?
- Item2 installation configuration and environment failure solution
- Crosslinked porphyrin based polyimide ppbpi-2, ppbpi-1-cr and ppbpi-2-cr; Porous porphyrin based hyperbranched polyimide (ppbpi-1, ppbpi-2) supplied by Qiyue
- 两水先木示身为Unity3D职场人的个人觉悟
- China's wind farm operation industry's "fourteenth five year plan" planning direction and investment risk prediction report 2022-2027
- Cache usage
- Database persistence
猜你喜欢
![[image detection] image saliency detection based on ITTI model with matlab code](/img/f3/a8b13431724059f8c8a77961778c67.png)
[image detection] image saliency detection based on ITTI model with matlab code

The performance of iron and steel enterprises was expected to be good in January this year. Since February, the prices of products of iron and steel enterprises have increased significantly. A mighty

二叉树中和为某一值的路径(一)(二)(三)(剑指offer)

GMP模型

【图像检测】基于形态学实现图像目标尺寸测量系统附matlab代码

ZRaQnHYDAe

基于sanic的服务使用celery完成动态修改定时任务
![[004] [stm32] MDK project configuration and commissioning](/img/a8/9817cdbbce557a92739de494490706.jpg)
[004] [stm32] MDK project configuration and commissioning

oracle创建带返回值的存储过程并sql执行调用

大厂面试TCP协议经典十五连问!22张图让你彻底弄明白
随机推荐
ZRaQnHYDAe
[cellular automata] Based on cellular automata, realize the traffic flow problem of expressway toll station, with matlab code
NumPy学习挑战第一关-NumPy的下载与安装
shell 输入验证仅限字母数字
i3wm 获取window class
Crosslinked metalloporphyrin based polyimide ppbpi-h) PPBP Mn; PBP-Fe; PPBPI-Fe-CR; Ppbpi Mn CR product - supplied by Qiyue
When asked during the interview, can redis master-slave copy not answer? These 13 pictures let you understand thoroughly
Redis系列——5种常见数据类型day1-3
【元胞自动机】基于元胞自动机实现高速公路收费站交通流问题附matlab代码
China's audio industry competition trend outlook and future development trend forecast report 2022 Edition
ES cluster_block_exception read_only_allow_delete问题
unity之EasyAR使用
Spark3.3.0 source code compilation supplement - Crazy certificate problem
Research Report on market development prospect and investment strategy of China's water soluble film industry 2022-2027
[004] [stm32] MDK project configuration and commissioning
China's Ni MH battery industry development overview analysis and investment trend forecast report 2022 Edition
【yolov4】基于yolov4深度学习网络目标检测MATLAB仿真
MySQL operation database
海量日志采集工具——Flume
NumPy学习挑战第五关-创建数组