zoukankan      html  css  js  c++  java
  • DevExpress GridView 单元格进度条的绑定及拓展自定义进度条不同颜色显示

    DevExpress GridView 单元格进度条的绑定及拓展自定义进度条不同颜色显示

    2019年06月21日 18:17:27 涛神-Dev 阅读数 34 标签: GridView单元格进度条 更多

    个人分类: DevExpressWinform

    DevExpress提供进度条的控件ProgressBarControl,并且能在GridView的单元格中使用,效果图如图所示

    关于单元格绑定progressBarControl,这里我简单介绍一下,列的ColumnEdit属性选择ProgressBarControl,然后设置选择的repositoryitemprogressBar1的ShowTitle属性来显示中间的百分数文本即可(其中,这里的数据源要求为整数型,如果你的字段值为小数类型的话,比如0.5,绑定字段的时候乘以100,改成50,这里的%是控件本身会显示的,不需要设置displayformat)。

    ProgressBarControl使用起来简单快捷,但是如果想控制当小于某个值的时候用颜色预警,ProgressbarControl暂时还没有提供这个功能,所以只能自己利用GridView的CustomDrawCell的单元格绘制事件来实现此需求。

    代码直接copy调用即可使用:

    
     
    1. /// <summary>

    2. /// 自定义进度条列

    3. /// </summary>

    4. /// <param name="view"></param>

    5. /// <param name="fieldName">列的字段名</param>

    6. /// <param name="warningValue"></param>

    7. /// <param name="lessColor"></param>

    8. /// <param name="greaterColor"></param>

    9. public static void CustomProgressBarColumn(DevExpress.XtraGrid.Views.Grid.GridView view, string fieldName, int warningValue=50, Brush lessColor = null, Brush greaterColor = null)

    10. {

    11. var col = view.Columns[fieldName];

    12. if (col == null) return;

    13. col.AppearanceCell.Options.UseTextOptions = true;

    14. col.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;

    15. view.CustomDrawCell += (s, e) =>

    16. {

    17. if (e.Column.FieldName == fieldName)

    18. {

    19. DrawProgressBar(e,warningValue,lessColor,greaterColor);

    20. e.Handled = true;

    21. DrawEditor(e);

    22. }

    23. };

    24. }

    25. static void DrawProgressBar(DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e, int warningValue = 50, Brush lessColor = null, Brush greaterColor = null)

    26. {

    27. decimal percent = e.CellValue == null ? 0m : (decimal)e.CellValue;

    28. int width = (int)(percent * e.Bounds.Width);

    29. Rectangle rect = new Rectangle(e.Bounds.X, e.Bounds.Y, width, e.Bounds.Height);

    30.  
    31. Brush b = Brushes.Green;

    32. if (greaterColor != null)

    33. {

    34. b = greaterColor;

    35. }

    36. if (percent * 100 < warningValue)

    37. {

    38. if (lessColor == null)

    39. {

    40. b = Brushes.Red;

    41. }

    42. else

    43. {

    44. b = lessColor;

    45. }

    46. }

    47. e.Graphics.FillRectangle(b, rect);

    48. }

    49. static void DrawEditor(DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)

    50. {

    51. GridCellInfo cell = e.Cell as GridCellInfo;

    52. Point offset = cell.CellValueRect.Location;

    53. BaseEditPainter pb = cell.ViewInfo.Painter as BaseEditPainter;

    54. AppearanceObject style = cell.ViewInfo.PaintAppearance;

    55. if (!offset.IsEmpty)

    56. cell.ViewInfo.Offset(offset.X, offset.Y);

    57. try

    58. {

    59. pb.Draw(new ControlGraphicsInfoArgs(cell.ViewInfo, e.Cache, cell.Bounds));

    60. }

    61. finally

    62. {

    63. if (!offset.IsEmpty)

    64. {

    65. cell.ViewInfo.Offset(-offset.X, -offset.Y);

    66. }

    67. }

    68. }

    调用代码示例如下(绑定的数据源字段值为小于1的小数值,如:0.33,设置列的DisplayFormat属性):

    
     
    1. gridView1.Columns["col11"].DisplayFormat.FormatType = FormatType.Numeric;

    2. gridView1.Columns["col11"].DisplayFormat.FormatString = "p0";

    3. CustomProgressBarColumn(gridView1, "col11", 50, Brushes.IndianRed, Brushes.DodgerBlue);

  • 相关阅读:
    定位中方向余弦矩阵(DCM)简介
    前端UI框架小汇总
    前端知识点小结
    overflow兼容iOS
    使用Flexible实现手淘H5页面的终端适配
    获取当前Javascript脚本文件的路径
    Javascript中document.execCommand()的用法 ( 实现浏览器菜单的很多功能 )
    jquery中的map()方法与js中的map()方法
    js判断是否为移动端
    页面制作注意事项
  • 原文地址:https://www.cnblogs.com/grj001/p/12225067.html
Copyright © 2011-2022 走看看