码迷,mamicode.com
首页 > 其他好文 > 详细

实现在DevExpress.XtraGrid.GridControl的列头绘制复选框以实现全选的功能

时间:2016-03-15 18:35:02      阅读:756      评论:0      收藏:0      [点我收藏+]

标签:

首先新建一个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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!