Dev+Grid复选框

public class GridControlHelp
{

/// <summary>
/// 是否选中
/// </summary>
private static bool chkState = false;
//复选框列名称
private static string chkFileName = "";
//复选框列宽
private static int chkWidth = 30;
//GridView
public static DevExpress.XtraGrid.Views.Grid.GridView GView = null;

private DevExpress.XtraGrid.Views.Grid.GridView gView
{
get
{
if (GView == null)
{
GView = new DevExpress.XtraGrid.Views.Grid.GridView();
}
return GView;
}
set
{
this.gView = value;
}
}

public static void GridCheckEdit(DevExpress.XtraGrid.Views.Grid.GridView gv, string checkFileName, int checkWidth)
{
if (gv != null)
{
chkFileName = checkFileName;
chkWidth = checkWidth;
GView = gv;
//不显示复选框的列标题
gv.Columns[chkFileName].OptionsColumn.ShowCaption = false;

//复选框的形状 gv.Columns[chkFileName].ColumnEdit 实例是 repositoryItemCheckEdit1
//repositoryItemCheckEdit1.CheckStyle = DevExpress.XtraEditors.Controls.CheckStyles.Standard;
////复选框加载的状态 实心 空心 空心打勾
//repositoryItemCheckEdit1.NullStyle = DevExpress.XtraEditors.Controls.StyleIndeterminate.Unchecked;
//点击事件
gv.Click += new System.EventHandler(gv_Click);
//画列头CheckEdit
gv.CustomDrawColumnHeader += new DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventHandler(gv_CustomDrawColumnHeader);
gv.DataSourceChanged += new EventHandler(gv_DataSourceChanged);
}
}

private static void gv_Click(object sender, EventArgs e)
{
if (ClickGridCheckBox(GView, chkFileName, chkState))
{
chkState = !chkState;
}
}

private static void gv_CustomDrawColumnHeader(object sender, DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e)
{
if (e.Column != null && e.Column.FieldName == chkFileName)
{
e.Info.InnerElements.Clear();
e.Painter.DrawObject(e.Info);
DrawCheckBox(e, chkState);
e.Handled = true;
}
}

private static void gv_DataSourceChanged(object sender, EventArgs e)
{
DevExpress.XtraGrid.Columns.GridColumn column = GView.Columns.ColumnByFieldName(chkFileName);
if (column != null)
{
column.Width = chkWidth;
column.OptionsColumn.ShowCaption = false;
column.ColumnEdit = new DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit();
}
}

private static void DrawCheckBox(DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e, bool chk)
{
DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit repositoryCheck = e.Column.ColumnEdit as DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit;
if (repositoryCheck != null)
{
System.Drawing.Graphics g = e.Graphics;
System.Drawing.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();
}
}

private 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;
System.Drawing.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;
}

}

调用

Grid的数据源是Table时,需要对Table进行处理,代码如下

//table.Columns.Add("Chk",System.Type.GetType("System.Boolean"));
//table.Columns["Chk"].DefaultValue=Boolean.FalseString;

同时需要在GridControl中添加列绑定FiledName为Chk,并设置ColumnEdit为CheckBox。

GridControl.DataSource=table;

GridCheckEdit(gridView,fieldName,checkWidth);

原文地址:https://www.cnblogs.com/stoneWl/p/10905924.html

时间: 2024-10-10 16:34:47

Dev+Grid复选框的相关文章

dev 表格添加复选框 ,可以勾选多行

this.repositoryItemCheckEdit1.QueryCheckStateByValue += new DevExpress.XtraEditors.Controls.QueryCheckStateByValueEventHandler(re3_QueryCheckStateByValue); //判断表格的数据勾选复选框 void re3_QueryCheckStateByValue(object sender, DevExpress.XtraEditors.Controls.

WPF使用DEV之TreeListControl---添加复选框

1.绑定的Class中添加一个Bool类型的属性 2.   该属性无需初始赋值,你运行即可看到如图: Bool类型数据自动变成了复选框 3.当然了,你肯定想知道用户选择了哪些复选框,从而你就想获取选中复选框对应的这一行后面的数据做操作 --这一点我当时很头疼,想有什么事件触发什么方法解决,感谢@常州木头 开导 --当你完成上述1时出现2,用户在在一些复选框打了对勾后,ObservableCollection<ConnectionModel>中对应Bool数据IsSelect就自动变为了true

WPF使用DEV之TreeListControl---加入复选框

1.绑定的Class中加入一个Bool类型的属性 2.   该属性无需初始赋值,你执行就可以看到如图: Bool类型数据自己主动变成了复选框 3.当然了,你肯定想知道用户选择了哪些复选框,从而你就想获取选中复选框相应的这一行后面的数据做操作 --这一点我当时非常头疼,想有什么事件触发什么方法解决,感谢@常州木头 开导 --当你完毕上述1时出现2,用户在在一些复选框打了对勾后,ObservableCollection<ConnectionModel>中相应Bool数据IsSelect就自己主动变

DEV控件的分页控件,实现勾选复选框

/// <summary> /// 单元格的点击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void gViewActPara_MouseDown(object sender, MouseEventArgs e) { GridHitInfo hint = Dwgd

extjs 中动态给gridpanel 复选框赋值

最近在搞extjs时需要动态根据数据给gridpanel的复选框赋值 网上看了很多 ,多不行,最后找到一个好使的方法 如下: RBACformPanel.getSelectionModel().select(0, true); 其中RBACformPanel就是你的gridpanel, select中的参数0表示grid第一列的对象,true表示将checkbox设置为true 即为勾选状态.

ExtJS表格——行号、复选框、选择模型

本篇的内容是为表格添加行号,和复选框,最后谈一下Ext的选择模型.内容比较简单,就直接上代码了.一. 设置行号   行号的设置主要问题在于删除某一行后需要重新计算行号  Ext.onReady(function() {         var cm = new Ext.grid.ColumnModel([                     new Ext.grid.RowNumberer(),           --在这里设置行号                    { header:

ExtJS4.2学习(五)表格渲染与复选框

鸣谢:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-11-10/174.html ---------------------------------------------------------------------------------------- 本博客介绍了如何对表格的列进行渲染及在行号前加入复选框. 以下是效果图: 代码: /** * Grid * 此js演示了如何对列的样式进行渲染以及如何为

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

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

WPF: 实现带全选复选框的列表控件

本文将说明如何创建一个带全选复选框的列表控件.其效果如下图:     这个控件是由一个复选框(CheckBox)与一个 ListView 组合而成.它的操作逻辑: 当选中“全选”时,列表中所有的项目都会被选中:反之,取消选中“全选”时,所有项都会被取消勾选. 在列表中选中部分数据项目时,“全选”框会呈现不确定状态(Indetermine). 由此看出,“全选”复选框与列表项中的复选框达到了双向控制的效果. 其设计思路:首先,创建自定义控件(CheckListView),在其 ControlTem