C#實(shí)現(xiàn)給DevExpress中GridView表格指定列添加進(jìn)度條
一、問(wèn)題描述
在我們使用Winform配合DevExpress進(jìn)行開(kāi)發(fā)表格時(shí),表格中的涉及到百分比數(shù)據(jù)的內(nèi)容除了顯示百分比的數(shù)字內(nèi)容外,還希望搭配顯示進(jìn)度條效果(且低于百分之60的內(nèi)容用紅色表示不合格,高于百分之60的用綠色表示),這樣百分比的顯示效果更加清晰直觀。


二、實(shí)現(xiàn)方法
2.1、先注冊(cè)單元格繪制方法


2.2、編寫(xiě)給指定單元格繪制進(jìn)度條的方法
#region 給表格指定列繪制進(jìn)度條
/// <summary>
/// 給指定列繪制進(jìn)度條
/// </summary>
/// <param name="gridView">GridView控件</param>
/// <param name="columnFieldName">需繪制進(jìn)度條列的字段名稱(chēng)</param>
/// <param name="warningValue">警告值(用于區(qū)分不同的顏色)</param>
/// <param name="beforeWaringValueColor">警告值前的進(jìn)度條顏色</param>
/// <param name="afterWaringValueColor">警告值后的進(jìn)度條顏色</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;
//設(shè)置該列顯示文本內(nèi)容的位置(這里默認(rèn)居中)
column.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
//繪制事件方法(前提需要先注冊(cè)才能夠接收到參數(shù)使用)
gridView.CustomDrawCell += (s, e) =>
{
if (e.Column.FieldName == columnFieldName)
{
DrawProgressBar(e, warningValue, beforeWaringValueColor, afterWaringValueColor);
e.Handled = true;
DrawEditor(e);
}
};
}
/// <summary>
/// 繪制進(jìn)度條
/// </summary>
/// <param name="e">單元格繪制事件參數(shù)</param>
/// <param name="warningValue">警告值(用于區(qū)分不同的顏色)</param>
/// <param name="beforeWaringValueColor">警告值前的進(jìn)度條顏色</param>
/// <param name="afterWaringValueColor">警告值后的進(jìn)度條顏色</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">單元格繪制事件參數(shù)</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、使用給指定單元格繪制進(jìn)度條方法
注意:在使用給指定單元格繪制進(jìn)度條方法時(shí)(如果數(shù)字都是小于1的那么警告值也是需要小于1;如果是大于1的則按照需要設(shè)置即可)。
使用方法語(yǔ)法
DrawProgressBarToColumn(gridView組件名稱(chēng), 需要繪制進(jìn)度條的單元格字段, 警告值,警告值前的進(jìn)度條顏色,警告值后的進(jìn)度條顏色);
示例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)));
三、相關(guān)內(nèi)容
3.1、給單元格設(shè)置百分比
/// <summary>
/// 設(shè)置表格指定單元格都使用百分比
/// </summary>
/// <param name="gridView">gridView組件</param>
/// <param name="columnName">列名稱(chēng)</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";
}
}
上面這個(gè)代碼實(shí)現(xiàn)的是將小數(shù)轉(zhuǎn)為百分比顯示【比如將小數(shù)(0.3)使用后就轉(zhuǎn)為(30%)顯示】
/// <summary>
/// 給表格指定單元格都保留2位小數(shù)后添加%號(hào)
/// </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}'%'";
}
}
}
上面這個(gè)代碼實(shí)現(xiàn)的是給數(shù)字添加百分號(hào)符號(hào)顯示【比如將小數(shù)(86.2356)使用后就轉(zhuǎn)為(86.24%)顯示】
以上就是C#實(shí)現(xiàn)給DevExpress中GridView表格指定列添加進(jìn)度條的詳細(xì)內(nèi)容,更多關(guān)于C# DevExpress GridView表格添加進(jìn)度條的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
AjaxControlToolkit AjaxFileUpload 顯示英文改成中文的解決方法
AjaxControlToolkit AjaxFileUpload 顯示英文改成中文的解決方法,需要的朋友可以參考一下2013-03-03
C#判斷指定驅(qū)動(dòng)器是否已經(jīng)準(zhǔn)備就緒的方法
這篇文章主要介紹了C#判斷指定驅(qū)動(dòng)器是否已經(jīng)準(zhǔn)備就緒的方法,涉及C#針對(duì)硬件IO操作的技巧,需要的朋友可以參考下2015-04-04
C#讀取QQ純真IP數(shù)據(jù)庫(kù)QQWry.Dat的代碼
QQ純真IP庫(kù)算是IP地址收集較為全的一個(gè)IP庫(kù),對(duì)于IP查詢來(lái)說(shuō)這個(gè)是不錯(cuò)的選擇。下面是如何查詢純真IP庫(kù)的一個(gè)類(lèi),使用C#代碼。2007-03-03
C#使用Dynamic實(shí)現(xiàn)簡(jiǎn)化反射
這篇文章主要為大家詳細(xì)介紹了C#如何使用Dynamic來(lái)實(shí)現(xiàn)簡(jiǎn)化反射,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-07-07
C#表達(dá)式樹(shù)Expression基礎(chǔ)講解
這篇文章介紹了C#表達(dá)式樹(shù)Expression和基本用法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12
C# Winfom 中ListBox的簡(jiǎn)單用法詳解
這篇文章主要介紹了C# Winfom 中ListBox的簡(jiǎn)單用法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12

