DevExpress GridControl 單元格添加進度條(ProgressBar)


首先可以使用DevExpress GridControl 自帶的進度條控件.

但是我要用一個方法來設置所以的單元格進度,而不是每個單元格都要設置一遍,同時我想要根據進度值不同,進度條顯示不同的顏色.

那么就要自己手動的編寫代碼來完成了.

1 : 繪制一個單元格進度條 形狀   當進度小於50%時顯示為紅色.

 1  public void DrawProgressBar(DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
 2         {
 3             string s = e.CellValue as string;
 4              s = s.Substring(0, e.CellValue.ToString().Length - 1);
 5              decimal percent = Convert.ToDecimal(s); 
 6             int width = (int)(100 * Math.Abs(percent /100 ) * e.Bounds.Width / 100);
 7             Rectangle rect = new Rectangle(e.Bounds.X, e.Bounds.Y, width, e.Bounds.Height);
 8             Brush b = Brushes.Green;
 9             if (percent < 50)
10             {
11                 b = Brushes.Red;
12             }
13             e.Graphics.FillRectangle(b, rect);
14         }

2 :  點擊 GridView 展開觸發事件

 1  private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
 2         {
 3             if (e.Column.FieldName == "CLASSPACE")
 4             {
 5                 DrawProgressBar(e);
 6 
 7                 e.Handled = true;
 8 
 9                 DrawEditor(e); 
10             } 
11         }

 

3 : 上面兩段代碼其實效果已經出來了,只不過有一些瑕疵,單元格只顯示數值,而不顯示進度條,(當點擊單元格時數值會消失),那么需要我們再來手動的編寫一段代碼用來處理當單元格觸發時一些操作.

代碼:

 1   private void DrawEditor(DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
 2         {
 3             GridCellInfo cell = e.Cell as GridCellInfo;
 4             Point offset = cell.CellValueRect.Location;
 5             BaseEditPainter pb = cell.ViewInfo.Painter as BaseEditPainter;
 6             AppearanceObject style = cell.ViewInfo.PaintAppearance;
 7             if (!offset.IsEmpty)
 8                 cell.ViewInfo.Offset(offset.X, offset.Y);
 9             try
10             {
11                 pb.Draw(new ControlGraphicsInfoArgs(cell.ViewInfo, e.Cache, cell.Bounds));
12             }
13             finally
14             {
15                 if(!offset.IsEmpty)
16                 {
17                     cell.ViewInfo.Offset(-offset.X, -offset.Y);
18                 }
19             }
20         }

 

同時 將 單元格設置為不可編輯狀態.

 

附 最后顯示效果 :


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM