当前位置:网站首页>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
边栏推荐
- PXRD, IR, TGA of two-dimensional porphyrin COF (POR COF) /cof (2D pdpor COF) - supplied by Qiyue
- 一文深入底层分析Redis对象结构
- 海量日志采集工具——Flume
- Numpy learning challenge level 5 - create array
- Paths with a certain value in a binary tree (1) (2) (3) (Sword finger offer)
- web入门之 Promise API
- es 中 mapping 简介
- MySQL
- Professional course - Code question record
- NumPy学习挑战第四关-NumPy数组属性
猜你喜欢

Redis系列——redis启动,客户端day1-2

MySQL basic usage 01

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

Paths with a certain value in a binary tree (1) (2) (3) (Sword finger offer)

【图像融合】基于耦合特征学习的多模式医学图像融合附matlab代码

5,10,15,20-tetraphenylporphyrin (TPP) and metal complexes fetpp/mntpp/cutpp/zntpp/nitpp/cotpp/pttpp/pdtpp/cdtpp supplied by Qiyue
![[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

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

Zraqnhydae

【图像增强】基于人工多重曝光融合AMEF实现图像去雾附matlab代码
随机推荐
Shell input validation alphanumeric only
Market scale forecast and investment risk forecast report of China's securities operating institutions 2022-2027
【路径规划】基于改进人工势场实现机器人路径规划附matlab代码
SQL
Here comes the apple ar/vr device exclusive system! Or named realityos
Liquid crystal texture diagram of purple solid mm-tpp-10c methacrylic acid decanoxy tetraphenyl porphyrin and mm-tpp-12c methacrylic acid dodecanoxy tetraphenyl porphyrin - Qi Yue display
[feature extraction] feature selection of target recognition information based on sparse PCA with Matlab source code
大厂面试TCP协议经典十五连问!22张图让你彻底弄明白
[004] [stm32] MDK project configuration and commissioning
MySQL 'replace into' 的坑 自增id,备机会有问题
Fmt Must the result of println (true) be true?
一芯实现喷雾|WS2812驱动|按键触摸|LED显示|语音播报芯片等功能,简化加湿器产品设计
MySQL
NumPy学习挑战第五关-创建数组
[cellular automata] Based on cellular automata, realize the traffic flow problem of expressway toll station, with matlab code
5,10,15,20-tetra (4-methoxycarbonylphenyl) porphyrin tcmpp purple crystal; Meso-5,10,15,20-tetra (4-methoxyphenyl) porphyrin tmopp|zn[t (4-mop) p] and co[t (4-mop) p] complexes
In depth analysis of redis object structure
ES字符串类型(Text vs keyword)的选择
Network IO, disk IO
Spark3.3.0 source code compilation supplement - Crazy certificate problem