1)DevExpress控件的GridView的实现多选操作

  先讲DevExpress控件的GridView的实现,要实现的功能基本上是处理单击全选操作、重新绘制表头等操作,首先在加载第一步实现相关的事件和操作,如下所示。

 this.gridView1.Click += new System.EventHandler(this.gridView1_Click);
  this.gridView1.CustomDrawColumnHeader += 
new DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventHandler
(this.gridView1_CustomDrawColumnHeader);
  this.gridView1.DataSourceChanged += 
new EventHandler(gridView1_DataSourceChanged);

  然后就是实现里面的事件操作了,对应的代码如下所示。

        private void gridView1_Click(object sender, EventArgs e)
         {
  if (DevControlHelper.ClickGridCheckBox(this.gridView1,  "Check", m_checkStatus))
             { 
                 m_checkStatus = !m_checkStatus; 
             } 
         }

private void gridView1_CustomDrawColumnHeader
(object sender, DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e) 
         { 
             if (e.Column != null  && e.Column.FieldName == "Check")
             { 
                 e.Info.InnerElements.Clear(); 
                 e.Painter.DrawObject(e.Info); 
                 DevControlHelper.DrawCheckBox(e, m_checkStatus); 
                 e.Handled = true; 
             } 
         } 
         void gridView1_DataSourceChanged(object sender, EventArgs e) 
         { 
             GridColumn column =
 this.gridView1.Columns.ColumnByFieldName( "Check");
             if (column != null) 
             { 
                 column.Width = 80; 
                 column.OptionsColumn.ShowCaption = false; 
                 column.ColumnEdit = new RepositoryItemCheckEdit(); 
             } 
        }

  其中单击和绘制表头的操作,交给另外一个类DevControlHelper来独立进行处理,数据源变化gridView1_DataSourceChanged实现的操作是寻找对应的全选列,并设置列宽、隐藏表头标题,并设置为复选框样式。

  DevControlHelper 类的实现代码如下所示:

        public static void DrawCheckBox
(DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e, bool chk)
         {
             RepositoryItemCheckEdit repositoryCheck = 
e.Column.ColumnEdit as RepositoryItemCheckEdit; 
             if (repositoryCheck != null) 
             { 
                 Graphics g = e.Graphics; 
                 Rectangle r = e.Bounds;

DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo info; 
                 DevExpress.XtraEditors.Drawing.CheckEditPainter painter; 
                 DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs args; 
                 info = repositoryCheck.CreateViewInfo() as
 DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo;

painter = repositoryCheck.CreatePainter() 
as DevExpress.XtraEditors.Drawing.CheckEditPainter; 
                 info.EditValue = chk; 
                 info.Bounds = r; 
                 info.CalcViewInfo(g); 
                 args = new DevExpress.XtraEditors.
Drawing.ControlGraphicsInfoArgs(info, new DevExpress.Utils.Drawing.GraphicsCache(g), r); 
                 painter.Draw(args); 
                 args.Cache.Dispose(); 
             } 
         }

public static bool ClickGridCheckBox
DevExpress.XtraGrid.Views.Grid.GridView gridView, string fieldName, bool currentStatus) 
         { 
             bool result = false; 
             if (gridView != null) 
             { 
                 gridView.ClearSorting();//禁止排序

gridView.PostEditor(); 
                 DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo info; 
                 Point pt = gridView.GridControl.PointToClient(Control.MousePosition); 
                 info = gridView.CalcHitInfo(pt); 
                 if (info.InColumn  && info.Column !=
 null && info.Column.FieldName == fieldName)
                 { 
                     for (int i = 0; i  < gridView.RowCount; i++)
                     { 
                         gridView.SetRowCellValue(i, fieldName, !currentStatus); 
                     } 
                     return true; 
                 } 
             } 
             return result; 
        } 

  2)传统DataGridView实现全选操作

  首先在第一列增加一个CheckBox控件,然后通过相关的事件,调整其位置,并相应对应的单击全选操作,初始化代码如下所示。

        CheckBox HeaderCheckBox = null;
         public FrmNormalGridViewSelect()
         { 
             InitializeComponent();

if (!this.DesignMode) 
             { 
                 HeaderCheckBox = new CheckBox(); 
                 HeaderCheckBox.Size = new Size(15, 15); 
                 this.dgvSelectAll.Controls.Add(HeaderCheckBox);

HeaderCheckBox.KeyUp += new KeyEventHandler(HeaderCheckBox_KeyUp); 
                 HeaderCheckBox.MouseClick += 
new MouseEventHandler(HeaderCheckBox_MouseClick); 
                 dgvSelectAll.CurrentCellDirtyStateChanged += 

new EventHandler(dgvSelectAll_CurrentCellDirtyStateChanged); 
                 dgvSelectAll.CellPainting +=
 new DataGridViewCellPaintingEventHandler(dgvSelectAll_CellPainting); 
             } 
    }

  事件实现了CheckBox重绘调整,并处理单击事件,如下所示。

        private void HeaderCheckBox_MouseClick(object sender, MouseEventArgs e)
         {
             HeaderCheckBoxClick((CheckBox)sender); 
         }

private void dgvSelectAll_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 
         { 
             if (e.RowIndex == -1  && e.ColumnIndex == 0)
                     ResetHeaderCheckBoxLocation(e.ColumnIndex, e.RowIndex); 
         }

private void ResetHeaderCheckBoxLocation(int ColumnIndex, int RowIndex) 
         { 
             Rectangle oRectangle = 
this.dgvSelectAll.GetCellDisplayRectangle(ColumnIndex, RowIndex, true); 
             Point oPoint = new Point(); 
             oPoint.X =
 oRectangle.Location.X + (oRectangle.Width - HeaderCheckBox.Width) / 2 + 1; 
             oPoint.Y =
 oRectangle.Location.Y + (oRectangle.Height - HeaderCheckBox.Height) / 2 + 1; 
             HeaderCheckBox.Location = oPoint; 
         }

private void HeaderCheckBoxClick(CheckBox HCheckBox) 
         { 
             foreach (DataGridViewRow Row in dgvSelectAll.Rows) 
             { 
                 ((DataGridViewCheckBoxCell)Row.Cells[ "chkBxSelect"]).Value = HCheckBox.Checked;
             } 
             dgvSelectAll.RefreshEdit(); 
    }

实现DataGridView和DevExpress.GridControl表头全选功能的更多相关文章

  1. GridControl表头全选操作实现之最优方法

    突然发现很久没有写博客了. 昨天整了个Windows Live Writer 就为了以后好好写写博客. 所以,开始咯. 为了积累,也为了分享. 之前在博客园中看到一篇文章:<Winform分页控 ...

  2. C# winform中的datagridview控件标头加入checkbox,实现全选功能。

    /// <summary> /// 给DataGridView添加全选 /// </summary> public class AddCheckBoxToDataGridVie ...

  3. js初学—实现checkbox全选功能

    布局如下: <p ><input type="checkbox" id="che1"/>全选</p><div id=& ...

  4. jquery实现全选功能

    主要是模拟一些网页中的表格实现全选功能. <form> 你爱好的运动是: <input type="checkbox" id="Check" ...

  5. Form - CHECKBOX全选功能

    FORM BUILDER开发,遇到这样一个需求: 添加一个CHECKBOX完成全选功能,红框为新添加的CHECKBOX(如图示) Try to use APP_RECORD.FOR_ALL_RECOR ...

  6. Android ListView条目全选功能,不用checkbox实现!

    大家好,翻了翻曾经的笔记,发现了一个我特别标记的功能,那就是ListView全选功能,顿时想起了我那个时候苦逼的生涯,因为我大学机械出身,大学毕业了都不知道什么叫代码,在58干了一段销售.实在是干不下 ...

  7. JS全选功能代码优化

    原文:JS全选功能代码优化 JS全选功能代码优化 最近在看javascript MVC那本书,也感觉到自己写的代码也并不优雅,所以一直在想 用另一种模式来编写JS代码,所以针对之前的简单的JS全选功能 ...

  8. S全选功能代码

    JS全选功能代码优化 2014-06-26 00:00 by 龙恩0707, 470 阅读, 3 评论, 收藏, 编辑 JS全选功能代码优化 最近在看javascript MVC那本书,也感觉到自己写 ...

  9. Java Swing 如何实现记事本中“编辑”菜单下的 剪切,复制,粘贴,删除,全选 功能

    这篇文字将要学习以下知识点: 1.如何给JButton按钮添加鼠标点击事件监听器 #1.addMouseListener(MouseListener l)  给JButton添加一个鼠标点击监听器l ...

随机推荐

  1. Co-prime Array&&Seating On Bus(两道水题)

     Co-prime Array Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Su ...

  2. [LeetCode] Interleaving String [30]

    题目 Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: ...

  3. ORM框架Hibernate (一) 对DAO封装和抽象

    说明 前面已经给大家介绍了Struts这个框架,Struts是对Web项目的表示层进行了封装,而Hibernate是对Web项目中DAO层进行封装,也即是.NET中我们常用到的D层封装,即对访问数据库 ...

  4. SpringMVC的值传递

    值的传递分为从页面传到到controller和从controller传递到页面,下面分别进行介绍: package com.springmvc.web; import java.util.Map; i ...

  5. cocos2d-x-2.2.5项目创建--命令行创建

    Gavin:downloads DavidLik$ cd cocos2d-x-2.2.5/ Gavin:cocos2d-x-2.2.5 DavidLik$ cd tools/ Gavin:tools ...

  6. 使用CSS达到阴阳八卦图等图形

    CSS还是比較强大的,能够实现中国古典的"阴阳八卦图"等形状. 正方形 #rectangle { width: 200px; height: 100px; backgrount-c ...

  7. JSP基础之 C标签中的 varStatues属性

    变量状态参数,该属性有5个常用值count 表示当前遍历集合的元素个数index 表示当前遍历到集合的第几个元素current 表示当前的集合元素first 表示集合的第一个元素last 表示集合的最 ...

  8. [STOI2014]舞伴(dp)

    STOI是汕头OI...无聊翻到了去年的比赛题目,就写然后自己测了一下. 其实我很想吐槽为什么题目名是perm,perm好像和舞伴完全无关.. dp(x,s)=∑dp(x-1,s-{i}))(0< ...

  9. JavaScript中setTimeout和setInterval的使用

    相同点:这两个方法都可以用来实现在一个固定的时间之后去实现JavaScript代码,两个方法都包含有两个参数,第一个是将要执行的代码字符串,第二是以毫秒为单位的时间间隔,当过了这个时间间隔之后就会执行 ...

  10. Thinking in Java——集合(Collection)

    一.ArrayList的使用(略) 二.容器的基本概念 (一).Collection是集合类的基本接口 主要方法: public interface Collection<E>{ bool ...