VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • C#教程之DevExpress实现GridControl列头绘制Checkbox的方

本文实例展示了DevExpress实现GridControl列头绘制Checkbox的方法,具体实现方法如下:

主要功能代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/// <summary>
/// 为列头绘制CheckBox
/// </summary>
/// <param name="view">GridView</param>
/// <param name="checkItem">RepositoryItemCheckEdit</param>
/// <param name="fieldName">需要绘制Checkbox的列名</param>
/// <param name="e">ColumnHeaderCustomDrawEventArgs</param>
public static void DrawHeaderCheckBox(this GridView view, RepositoryItemCheckEdit checkItem, string fieldName, ColumnHeaderCustomDrawEventArgs e)
{
  /*说明:
   *参考:https://www.devexpress.com/Support/Center/Question/Details/Q354489
   *在CustomDrawColumnHeader中使用
   *eg:
   * private void gvCabChDetail_CustomDrawColumnHeader(object sender, DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e)
   * {
   * GridView _view = sender as GridView;
   * _view.DrawHeaderCheckBox(CheckItem, "Check", e);
   * }
   */
  if (e.Column != null && e.Column.FieldName.Equals(fieldName))
  {
 e.Info.InnerElements.Clear();
 e.Painter.DrawObject(e.Info);
 DrawCheckBox(checkItem, e.Graphics, e.Bounds, getCheckedCount(view, fieldName) == view.DataRowCount);
 e.Handled = true;
  }
}
private static void DrawCheckBox(RepositoryItemCheckEdit checkItem, Graphics g, Rectangle r, bool Checked)
{
  CheckEditViewInfo _info;
  CheckEditPainter _painter;
  ControlGraphicsInfoArgs _args;
  _info = checkItem.CreateViewInfo() as CheckEditViewInfo;
  _painter = checkItem.CreatePainter() as CheckEditPainter;
  _info.EditValue = Checked;
 
  _info.Bounds = r;
  _info.PaintAppearance.ForeColor = Color.Black;
  _info.CalcViewInfo(g);
  _args = new ControlGraphicsInfoArgs(_info, new DevExpress.Utils.Drawing.GraphicsCache(g), r);
  _painter.Draw(_args);
  _args.Cache.Dispose();
}
private static int getCheckedCount(GridView view, string filedName)
{
  int count = 0;
  for (int i = 0; i < view.DataRowCount; i++)
  {
 object _cellValue = view.GetRowCellValue(i, view.Columns[filedName]);
 if (_cellValue == null) continue;
 if (string.IsNullOrEmpty(_cellValue.ToString().Trim())) continue;
 bool _checkStatus = false;
 if (bool.TryParse(_cellValue.ToString(), out _checkStatus))
 {
   if (_checkStatus)
 count++;
 }
  }
  return count;
}

代码使用方法如下:

?
1
2
3
4
5
6
7
RepositoryItemCheckEdit CheckItem = new RepositoryItemCheckEdit();
const string gcCheckFieldName = "Checked";
private void gvLampConfig_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
{
  GridView _view = sender as GridView;
  _view.DrawHeaderCheckBox(CheckItem, gcCheckFieldName, e);
}

代码运行效果如下图所示:


相关教程