当前位置:网站首页>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
边栏推荐
- Massive log collection tool flume
- Meso tetra (4-bromophenyl) porphyrin (tbpp); 5,10,15,20-tetra (4-methoxy-3-sulfonylphenyl) porphyrin [t (4-mop) ps4] supplied by Qiyue
- SQL
- [004] [stm32] MDK project configuration and commissioning
- How can I find the completely deleted photos in Apple mobile phone?
- 【路径规划】基于改进人工势场实现机器人路径规划附matlab代码
- Easyar use of unity
- Cocoscreator plays spine animation
- 【图像增强】基于人工多重曝光融合AMEF实现图像去雾附matlab代码
- MATLAB线性规划模型学习笔记
猜你喜欢

i3wm 获取window class
![[image enhancement] image defogging based on artificial multiple exposure fusion amef with matlab code](/img/4e/3317b2b99fc8da4d8af190f402e696.png)
[image enhancement] image defogging based on artificial multiple exposure fusion amef with matlab code

13. Mismatch simulation of power synthesis for ads usage recording

【yolov4】基于yolov4深度学习网络目标检测MATLAB仿真

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

Pytorch builds CNN LSTM hybrid model to realize multivariable and multi step time series forecasting (load forecasting)

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

【推荐一款实体类转换工具 MapStruct,性能强劲,简单易上手 】
随机推荐
What is data mining?
炒股怎么选择证券公司?手机开户安全么?
Here comes the apple ar/vr device exclusive system! Or named realityos
How to open an account in flush? Is it safe to open an account online?
PyTorch搭建CNN-LSTM混合模型实现多变量多步长时间序列预测(负荷预测)
ZRaQnHYDAe
Analyze 5 indicators of NFT project
Detailed materials of applying for residence permit in non local Beijing
Easyar use of unity
【图像增强】基于人工多重曝光融合AMEF实现图像去雾附matlab代码
SQL 查询语句
SQL
Shell input validation alphanumeric only
3,3 '- di (3,4-dicarboxyphenoxy) -4,4' - diphenylethynylbiphenyldianhydride (bpebpda) / porphyrin 2dcofs (H2P COF, ZNP COF and cup COF) supplied by Qiyue
Network IO, disk IO
Big factory interview TCP protocol classic 15 consecutive questions! 22 pictures to make you fully understand
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
Meso tetra (4-bromophenyl) porphyrin (tbpp); 5,10,15,20-tetra (4-methoxy-3-sulfonylphenyl) porphyrin [t (4-mop) ps4] supplied by Qiyue
item2安装配置及环境失效问题解决
C#实现给DevExpress中GridView表格指定列添加进度条显示效果——代码实现方式