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. 详细介绍Qt,ffmpeg 和SDL开发

        Qt 与 ffmpeg 与 SDl 教程是本文要介绍的内容,从多个角度介绍本文,运用了qmake,先来看内容. 1.  注释 从“ #” 开始,到这一行结束. 2.  指定源文件 1.     ...

  2. LR实战之Discuz开源论坛——安装及简介

    想了很久,也许是因为这段时间特别闲,从毕业到现在,我的测试职业生涯也近两年了,发现自己越来越喜欢测试领域,也越来越偏向测试开发了,作为一名专业的测试人员,不得不要学习性能测试,而使用LoadRunne ...

  3. Animate.css 教程

    animate.css 是一个有趣,酷炫的,跨浏览器的动画库,你可以将它用于你的项目中.不管是主页,滑动切换,又或者是其它方面,你都可以通过它来制作出惊人的效果. 基本用法 引入CSS文件 这个对你来 ...

  4. 转:Xshell显示找不到匹配的outgoing encryption算法怎么办

    原文出处:http://www.xshellcn.com/xsh_column/suanfa-bpp.html 由用户反应在使用xshell和xftp连接debian 7时出现找不到匹配的outgoi ...

  5. CSS-边框-效果

    1.1边框 其中边框圆角.边框阴影属性,应用十分广泛,兼容性也相对较好,具有符合渐进增强原则的特性,我们需要重点掌握. 1.1.1边框圆角 border-radius 每个角可以设置两个值,x值,y值 ...

  6. Android Studio Errors

    1.The import org.apache.http.client; tip: cannot be resolved; resolve: add this line in build.gradle ...

  7. ThinkPHP 类似Yii的Gii生成Model的功能。

    ThinkPHP 类似Yii的Gii生成Model的功能.自动生成ThinkPhp 3.1 的基础模型.. #!/usr/bin/env php <?php /** * * THINKPHP 基 ...

  8. AdapterView及其子类之三:基于ListView及ArrayAdapter实现列表

    见归档项目ListViewDemo.zip. 基本步骤如下: 1.创建主布局文件,里面包含一个ListView元素. <RelativeLayout xmlns:android="ht ...

  9. 菜鸟的jQuery源码学习笔记(二)

    jQuery对象是使用构造函数和原型模式相结合的方式创建的.现在来看看jQuery的原型对象jQuery.prototype: jQuery.fn = jQuery.prototype = { //成 ...

  10. 第一局 ThreeJS-开始

    本文介绍ThreeJS使用的大体流程.(由于水平有限,请大家多多指教.) 1.ThreeJS下载和引入: (1)下载地址:https://github.com/mrdoob/three.js/arch ...