亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#實現(xiàn)給DevExpress中GridView表格指定列添加進度條

 更新時間:2022年06月29日 16:03:09   作者:牛奶咖啡13  
這篇文章主要為大家詳細(xì)介紹了如何利用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)容的位置(這里默認(rèn)居中)
            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表格指定列添加進度條的詳細(xì)內(nèi)容,更多關(guān)于C# DevExpress GridView表格添加進度條的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 綁定winform中DataGrid

    綁定winform中DataGrid

    綁定winform中DataGrid,需要的朋友可以參考一下
    2013-02-02
  • AjaxControlToolkit AjaxFileUpload 顯示英文改成中文的解決方法

    AjaxControlToolkit AjaxFileUpload 顯示英文改成中文的解決方法

    AjaxControlToolkit AjaxFileUpload 顯示英文改成中文的解決方法,需要的朋友可以參考一下
    2013-03-03
  • C#設(shè)置輸入法實例分析

    C#設(shè)置輸入法實例分析

    這篇文章主要介紹了C#設(shè)置輸入法的方法,實例分析了C#獲取系統(tǒng)輸入法及設(shè)置輸入法的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • C#判斷指定驅(qū)動器是否已經(jīng)準(zhǔn)備就緒的方法

    C#判斷指定驅(qū)動器是否已經(jīng)準(zhǔn)備就緒的方法

    這篇文章主要介紹了C#判斷指定驅(qū)動器是否已經(jīng)準(zhǔn)備就緒的方法,涉及C#針對硬件IO操作的技巧,需要的朋友可以參考下
    2015-04-04
  • C#讀取QQ純真IP數(shù)據(jù)庫QQWry.Dat的代碼

    C#讀取QQ純真IP數(shù)據(jù)庫QQWry.Dat的代碼

    QQ純真IP庫算是IP地址收集較為全的一個IP庫,對于IP查詢來說這個是不錯的選擇。下面是如何查詢純真IP庫的一個類,使用C#代碼。
    2007-03-03
  • C#使用Dynamic實現(xiàn)簡化反射

    C#使用Dynamic實現(xiàn)簡化反射

    這篇文章主要為大家詳細(xì)介紹了C#如何使用Dynamic來實現(xiàn)簡化反射,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以了解一下
    2023-07-07
  • C#表達式樹Expression基礎(chǔ)講解

    C#表達式樹Expression基礎(chǔ)講解

    這篇文章介紹了C#表達式樹Expression和基本用法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-12-12
  • C#判斷字符是否為漢字的三種方法分享

    C#判斷字符是否為漢字的三種方法分享

    判斷一個字符是不是漢字通常有三種方法,第一種用 ASCII 碼判斷,第二種用漢字的 UNICODE 編碼范圍判 斷,第三種用正則表達式判斷,以下是具體方法
    2014-01-01
  • C#9.0:Init相關(guān)總結(jié)

    C#9.0:Init相關(guān)總結(jié)

    這篇文章主要介紹了C#9.0:Init的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)新版的c#,感興趣的朋友可以了解下
    2021-02-02
  • C# Winfom 中ListBox的簡單用法詳解

    C# Winfom 中ListBox的簡單用法詳解

    這篇文章主要介紹了C# Winfom 中ListBox的簡單用法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12

最新評論