C#實現(xiàn)給DevExpress中GridView表格指定列添加進度條
一、問題描述
在我們使用Winform配合DevExpress進行開發(fā)表格時,表格中的涉及到百分比數(shù)據(jù)的內(nèi)容除了顯示百分比的數(shù)字內(nèi)容外,還希望搭配顯示進度條效果(且低于百分之60的內(nèi)容用紅色表示不合格,高于百分之60的用綠色表示),這樣百分比的顯示效果更加清晰直觀。
二、實現(xiàn)方法
2.1、先注冊單元格繪制方法
2.2、編寫給指定單元格繪制進度條的方法
#region 給表格指定列繪制進度條 /// <summary> /// 給指定列繪制進度條 /// </summary> /// <param name="gridView">GridView控件</param> /// <param name="columnFieldName">需繪制進度條列的字段名稱</param> /// <param name="warningValue">警告值(用于區(qū)分不同的顏色)</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; //設(shè)置該列顯示文本內(nèi)容的位置(這里默認居中) column.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center; //繪制事件方法(前提需要先注冊才能夠接收到參數(shù)使用) gridView.CustomDrawCell += (s, e) => { if (e.Column.FieldName == columnFieldName) { DrawProgressBar(e, warningValue, beforeWaringValueColor, afterWaringValueColor); e.Handled = true; DrawEditor(e); } }; } /// <summary> /// 繪制進度條 /// </summary> /// <param name="e">單元格繪制事件參數(shù)</param> /// <param name="warningValue">警告值(用于區(qū)分不同的顏色)</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">單元格繪制事件參數(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、使用給指定單元格繪制進度條方法
注意:在使用給指定單元格繪制進度條方法時(如果數(shù)字都是小于1的那么警告值也是需要小于1;如果是大于1的則按照需要設(shè)置即可)。
使用方法語法
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)));
三、相關(guān)內(nèi)容
3.1、給單元格設(shè)置百分比
/// <summary> /// 設(shè)置表格指定單元格都使用百分比 /// </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"; } }
上面這個代碼實現(xiàn)的是將小數(shù)轉(zhuǎn)為百分比顯示【比如將小數(shù)(0.3)使用后就轉(zhuǎn)為(30%)顯示】
/// <summary> /// 給表格指定單元格都保留2位小數(shù)后添加%號 /// </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}'%'"; } } }
上面這個代碼實現(xiàn)的是給數(shù)字添加百分號符號顯示【比如將小數(shù)(86.2356)使用后就轉(zhuǎn)為(86.24%)顯示】
以上就是C#實現(xiàn)給DevExpress中GridView表格指定列添加進度條的詳細內(nèi)容,更多關(guān)于C# DevExpress GridView表格添加進度條的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
AjaxControlToolkit AjaxFileUpload 顯示英文改成中文的解決方法
AjaxControlToolkit AjaxFileUpload 顯示英文改成中文的解決方法,需要的朋友可以參考一下2013-03-03C#判斷指定驅(qū)動器是否已經(jīng)準備就緒的方法
這篇文章主要介紹了C#判斷指定驅(qū)動器是否已經(jīng)準備就緒的方法,涉及C#針對硬件IO操作的技巧,需要的朋友可以參考下2015-04-04C#讀取QQ純真IP數(shù)據(jù)庫QQWry.Dat的代碼
QQ純真IP庫算是IP地址收集較為全的一個IP庫,對于IP查詢來說這個是不錯的選擇。下面是如何查詢純真IP庫的一個類,使用C#代碼。2007-03-03