标签:
首先新建一个Win Form测试项目,拖一个GridControl控件到窗体上。
1 public partial class Form1 : Form 2 { 3 public Form1() 4 { 5 InitializeComponent(); 6 } 7 8 private Rectangle checkBoxColumnHeaderRect = Rectangle.Empty; 9 private GridColumn checkBoxColumn = null; 10 private void gridView1_CustomDrawColumnHeader(object sender, DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e) 11 { 12 if (e.Column != null && e.Column.AbsoluteIndex == 0) 13 { 14 checkBoxColumnHeaderRect = e.Bounds; 15 checkBoxColumn = e.Column; 16 17 //须把列头标题设置为空 18 e.Painter.DrawObject(e.Info); 19 20 //在列头中心显示复选框 21 int x = e.Bounds.X + (int)((e.Bounds.Width - CheckBoxRenderer.GetGlyphSize(e.Graphics, CheckBoxState.UncheckedNormal).Width) * 0.5); 22 int y = e.Bounds.Y + (int)((e.Bounds.Height - CheckBoxRenderer.GetGlyphSize(e.Graphics, CheckBoxState.UncheckedNormal).Height) * 0.5); 23 Point location = new Point(x, y); 24 25 CheckBoxState checkBoxState; 26 if (e.Column.Tag != null && e.Column.Tag.ToString() == "1") 27 checkBoxState = CheckBoxState.CheckedPressed; 28 else 29 checkBoxState = CheckBoxState.UncheckedNormal; 30 31 CheckBoxRenderer.DrawCheckBox(e.Graphics, location, checkBoxState); 32 33 e.Handled = true; 34 } 35 } 36 37 private void gridView1_MouseUp(object sender, MouseEventArgs e) 38 { 39 if (checkBoxColumnHeaderRect != Rectangle.Empty) 40 { 41 if (e.X > checkBoxColumnHeaderRect.X && e.X < (checkBoxColumnHeaderRect.X + checkBoxColumnHeaderRect.Width) && e.Y > checkBoxColumnHeaderRect.Y && e.Y < (checkBoxColumnHeaderRect.Y + checkBoxColumnHeaderRect.Height)) 42 { 43 if (checkBoxColumn.Tag != null && checkBoxColumn.Tag.ToString() == "1") 44 checkBoxColumn.Tag = "0"; 45 //在这写未全选逻辑 46 else 47 checkBoxColumn.Tag = "1"; 48 //在这写全选逻辑 49 gridView1.InvalidateColumnHeader(checkBoxColumn); 50 } 51 } 52 } 53 }
运行效果:
实现在DevExpress.XtraGrid.GridControl的列头绘制复选框以实现全选的功能
标签:
原文地址:http://www.cnblogs.com/jiao1855/p/5280430.html