开发者论坛

 找回密码
 注册 (请使用非IE浏览器)
查看: 2347|回复: 0

向GridControl添加进度条控件

[复制链接]

0

精华

8

贡献

1768

赞扬

特约版主

帖子
583
软币
4524
在线时间
275 小时
注册时间
2019-2-21
发表于 2019-3-18 11:46:20 | 显示全部楼层 |阅读模式

本文将为大家介绍如何在DevExpress GridControl中添加进度条控件。

DXperience Universal Suite下载

首先可以使用 DevExpress GridControl 自带的进度条控件。

但是我要用一个方法来设置所有的单元格进度,而不是每个单元格都要设置一遍,同时我想要根据进度值不同,进度条显示不同的颜色。

那么就要自己手动的编写代码来完成了。

1 、绘制一个单元格进度条形状,当进度小于50%时显示为红色。

[C#] 纯文本查看 复制代码
public void DrawProgressBar(DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            string s = e.CellValue as string;
             s = s.Substring(0, e.CellValue.ToString().Length - 1);
             decimal percent = Convert.ToDecimal(s);
            int width = (int)(100 * Math.Abs(percent /100 ) * e.Bounds.Width / 100);
            Rectangle rect = new Rectangle(e.Bounds.X, e.Bounds.Y, width, e.Bounds.Height);
            Brush b = Brushes.Green;
            if (percent < 50)
            {
                b = Brushes.Red;
            }
            e.Graphics.FillRectangle(b, rect);
        }

2、点击 GridView 展开触发事件

[C#] 纯文本查看 复制代码
private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column.FieldName == "CLASSPACE")
            {
                DrawProgressBar(e);
 
                e.Handled = true;
 
                DrawEditor(e);
            }
        }

3、上面两段代码其实效果已经出来了,只不过有一些瑕疵,单元格只显示数值,而不显示进度条(当点击单元格时数值会消失),那么需要我们再来手动的编写一段代码用来处理当单元格触发时一些操作。

[C#] 纯文本查看 复制代码
private void DrawEditor(DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            GridCellInfo cell = e.Cell as GridCellInfo;
            Point offset = cell.CellValueRect.Location;
            BaseEditPainter pb = cell.ViewInfo.Painter as BaseEditPainter;
            AppearanceObject style = cell.ViewInfo.PaintAppearance;
            if (!offset.IsEmpty)
                cell.ViewInfo.Offset(offset.X, offset.Y);
            try
            {
                pb.Draw(new ControlGraphicsInfoArgs(cell.ViewInfo, e.Cache, cell.Bounds));
            }
            finally
            {
                if(!offset.IsEmpty)
                {
                    cell.ViewInfo.Offset(-offset.X, -offset.Y);
                }
            }
        }

同时将单元格设置为不可编辑状态。

出处:http://www.cnblogs.com/Albin/



评分

参与人数 1赞扬 +1 收起 理由
kevenme + 1 感谢分享

查看全部评分

回复

使用道具 举报

Archiver|手机版|小黑屋|开发者网 ( 苏ICP备08004430号-2 )
版权所有:南京韵文教育信息咨询有限公司

GMT+8, 2024-4-20 10:48

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表