实现DataGridView和DevExpress.GridControl表头全选功能
1)DevExpress控件的GridView的实现多选操作
先讲DevExpress控件的GridView的实现,要实现的功能基本上是处理单击全选操作、重新绘制表头等操作,首先在加载第一步实现相关的事件和操作,如下所示。
this.gridView1.CustomDrawColumnHeader +=
new DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventHandler
(this.gridView1_CustomDrawColumnHeader);
this.gridView1.DataSourceChanged +=
new EventHandler(gridView1_DataSourceChanged);
然后就是实现里面的事件操作了,对应的代码如下所示。
{
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 类的实现代码如下所示:
(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控件,然后通过相关的事件,调整其位置,并相应对应的单击全选操作,初始化代码如下所示。
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 +=
dgvSelectAll.CellPainting +=
new DataGridViewCellPaintingEventHandler(dgvSelectAll_CellPainting);
}
}
事件实现了CheckBox重绘调整,并处理单击事件,如下所示。
{
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();
}
转载于:https://www.cnblogs.com/zhangruisoldier/p/4242351.html
实现DataGridView和DevExpress.GridControl表头全选功能相关推荐
- tableView表格重写表头增加全选功能和实现翻页(读写excel和读ini)
一:重写表头文件 HeaderView.h #pragma once #ifndef HEADERVIEW_H #define HEADERVIEW_H#include <QObject> ...
- DevExpress中实现单选、多选、全选功能
一.实现效果 GirdControl表格实现单选.多选.全选功能工程下载 实现对GridControl中的数据行单选.多选.全选功能,且能够获取到所有选择行的数据信息. 二.核心实现 2.1.在表格第 ...
- Vue+Element组件el-table添加表头全选文字
html部分: <el-table v-if="showTable"class="inter_table":data="apiList.slic ...
- 原生js实现table 横向纵向全选功能
要实现这种功能,很明显用elment ui实现不了.element ui 纵向需要prop名字不一样才能展示,很明显,我这个一层二层是名字一样的数组 期望后台返回数组 是这种格式. 所有需要自己用ta ...
- layui table 全选过滤_layui表格(表单)的全选功能
最近在使用layui这个框架,其中有用到表格的全选功能,下面记录下我的使用.html文件需要引用的是layui.js和layui.css,但是上面两个文件又会引入其他文件.,如图所示, 所以建议从la ...
- 表格全选和取消全选功能实现
1.创建HTML表格` ` <td><input type="checkbox" class="th_b"></td>< ...
- layui表格监听全选_layui表格(表单)的全选功能
最近在使用layui这个框架,其中有用到表格的全选功能,下面记录下我的使用.html文件需要引用的是layui.js和layui.css,但是上面两个文件又会引入其他文件.,如图所示, 所以建议从la ...
- layui 实现表单、表格中复选框checkbox的全选功能
一.layui 实现表单中多选框的全选功能,代码如下: //html页面表单 <form class="layui-form"><div class=" ...
- java记事本复制粘贴_Java Swing 如何实现记事本中“编辑”菜单下的 剪切,复制,粘贴,删除,全选 功能...
这篇文字将要学习以下知识点: 1.如何给JButton按钮添加鼠标点击事件监听器 #1.addMouseListener(MouseListener l) 给JButton添加一个鼠标点击监听器l ...
最新文章
- 如何动态修改windows下的host文件
- 关于C#写的记事本中一个问题
- 技巧速看!如何帮助“表哥”快速玩转报告美化?
- Bloom Filter(布隆过滤器)的概念和原理
- JavaWeb(part1)--servlet
- 基于visual Studio2013解决C语言竞赛题之0505选数
- 阿里员工的这则控诉,在圈内火了!
- 【Kafka】Kafka Leader:none ISR 为空 消费超时
- 阿里云矢量图html页面引入,使用阿里矢量图标库的三种方法
- Mac大小写切换,中英文切换
- 计算机硬盘坏道有什么特点,硬盘坏道对电脑会造成什么影响
- [编程题] 庆祝61
- 2018东北四省赛 Spell Boost DP
- 文本学习-《背影》-朱自清
- 零基础入门学习Python视频(全42集)
- TensorFlow的Object Detection目标检测报错 google.protobuf.text_format.ParseError:
- NAT地址转换(超详细解说版)
- 推荐系统CTR技术的演变历史
- 一文读懂 MySQL 底层架构实现
- 为什么拼多多、趣头条、小红书工资高?