GridControl表格中根据数据显示进度条

转自:https://www.cnblogs.com/zeroone/p/4311149.html(原文中讲述的比较详细,赞!)

效果如下

主要是三步骤:

1.表格的CustomDrawCell事件

2.DrawProgressBar方法

3.DrawEditor方法

代码如下:

private void bandedGridView2_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column == bandedGridColumnAA || e.Column == bandedGridColumnBA || e.Column == bandedGridColumnCA)//电流预警600,bandedGridColumnAA 为gridControl中的列名
            {
                e.Appearance.DrawBackground(e.Cache, e.Bounds);
                DrawProgressBar(e, 600);
                e.Handled = true;//避免选中行的颜色覆盖画上去的长方形ProgressBar
                DrawEditor(e);//赋值
            }
                
        }
        
        //绘制矩形进度条
        private void DrawProgressBar(DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e,decimal data)
        {
            decimal percent = Convert.ToDecimal(e.CellValue)/data;
            int 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.LawnGreen;
            if (percent < 1)
                b = Brushes.LawnGreen;
            
            else
                b = Brushes.MediumVioletRed;
            e.Graphics.FillRectangle(b, rect);
        }

        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 savedStyle = 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);
            }
        }

猜你喜欢

转载自blog.csdn.net/Tiger_shl/article/details/82414059