首頁 > 軟體

C#實現給DevExpress中GridView表格指定列新增進度條

2022-06-29 22:01:33

一、問題描述

在我們使用Winform配合DevExpress進行開發表格時,表格中的涉及到百分比資料的內容除了顯示百分比的數位內容外,還希望搭配顯示進度條效果(且低於百分之60的內容用紅色表示不合格,高於百分之60的用綠色表示),這樣百分比的顯示效果更加清晰直觀。

二、實現方法

2.1、先註冊單元格繪製方法

2.2、編寫給指定單元格繪製進度條的方法

        #region   給表格指定列繪製進度條
 
        /// <summary>
        /// 給指定列繪製進度條
        /// </summary>
        /// <param name="gridView">GridView控制元件</param>
        /// <param name="columnFieldName">需繪製進度條列的欄位名稱</param>
        /// <param name="warningValue">警告值(用於區分不同的顏色)</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;
            //設定該列顯示文字內容的位置(這裡預設居中)
            column.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            //繪製事件方法(前提需要先註冊才能夠接收到引數使用)
            gridView.CustomDrawCell += (s, e) =>
            {
                if (e.Column.FieldName == columnFieldName)
                {
                    DrawProgressBar(e, warningValue, beforeWaringValueColor, afterWaringValueColor);
                    e.Handled = true;
                    DrawEditor(e);
                }
            };
        }
 
        /// <summary>
        /// 繪製進度條
        /// </summary>
        /// <param name="e">單元格繪製事件引數</param>
        /// <param name="warningValue">警告值(用於區分不同的顏色)</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">單元格繪製事件引數</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、使用給指定單元格繪製進度條方法

注意:在使用給指定單元格繪製進度條方法時(如果數位都是小於1的那麼警告值也是需要小於1;如果是大於1的則按照需要設定即可)。

使用方法語法

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)));

三、相關內容

3.1、給單元格設定百分比

        /// <summary>
        /// 設定表格指定單元格都使用百分比
        /// </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";
            }
        }

上面這個程式碼實現的是將小數轉為百分比顯示【比如將小數(0.3)使用後就轉為(30%)顯示】 

        /// <summary>
        /// 給表格指定單元格都保留2位小數後新增%號
        /// </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}'%'";
 
                }
            }
        }

上面這個程式碼實現的是給數位新增百分號符號顯示【比如將小數(86.2356)使用後就轉為(86.24%)顯示】 

以上就是C#實現給DevExpress中GridView表格指定列新增進度條的詳細內容,更多關於C# DevExpress GridView表格新增進度條的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com