Devexpress GridView增加CheckBox列

参考DEV官网代码做了一个增加checkbox列效果:

#region 方法:设置GridView数据绑定
        public void GridDataBind()
        {
            dt = dbHelper.Fill(cmdStr);                     // 填充DataTable
            dt.Columns.Add("Check", typeof(bool));  // 增加个CheckBox列
            dt.Columns["Check"].SetOrdinal(0);       // 设置选择列的位置
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i]["Check"] = false;
            }
            gridControl1.DataSource = dt;
            gridView1.Columns["Check"].Width = 22;
            gridView1.Columns["Check"].OptionsColumn.ShowCaption = false;
            gridView1.Columns["Check"].OptionsColumn.AllowSort = DevExpress.Utils.DefaultBoolean.False;
        }
  #endregion

===================================================================================

#region GridView增加CheckBox列相关代码

protected void DrawCheckBox(Graphics g, Rectangle r, bool Checked)

{

DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo info;

DevExpress.XtraEditors.Drawing.CheckEditPainter painter;

DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs args;

info = chkEdit.CreateViewInfo() as DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo;

painter = chkEdit.CreatePainter() as DevExpress.XtraEditors.Drawing.CheckEditPainter;

info.EditValue = Checked;

info.Bounds = r;

info.PaintAppearance.ForeColor = Color.Black;

info.CalcViewInfo(g);

args = new DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs(info, new DevExpress.Utils.Drawing.GraphicsCache(g), r);

painter.Draw(args);

args.Cache.Dispose();

}

private void gridView_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)

{

if (e.Column == (sender as GridView).Columns["Check"])

{

e.Info.InnerElements.Clear();

e.Info.Appearance.ForeColor = Color.Blue;

e.Painter.DrawObject(e.Info);

DrawCheckBox(e.Graphics, e.Bounds, GetCheckedCount() == gridView1.DataRowCount);

//DrawCheckBox(e.Graphics, e.Bounds, false);

e.Handled = true;

}

}

private void gridView_MouseUp(object sender, MouseEventArgs e)

{

if (e.Clicks == 1 && e.Button == MouseButtons.Left)

{

GridHitInfo gridHitInfo;

Point point = gridView1.GridControl.PointToClient(Control.MousePosition);

gridHitInfo = gridView1.CalcHitInfo(point);

if (gridHitInfo.InColumn && gridHitInfo.Column.FieldName == "Check")

{

if (GetCheckedCount() == gridView1.DataRowCount)

{

UnCheckAll();

}

else

{

CheckAll();

}

}

if (gridHitInfo.InRow && gridHitInfo.InRowCell == true)

{

if (Convert.ToBoolean(gridView1.GetFocusedRowCellValue("Check")) == false)

{

CheckSingle();

}

else

{

UnCheckSingle();

}

}

}

}

private int GetCheckedCount() // 获取到已选择的数量

{

int count = 0;

for (int i = 0; i < gridView1.DataRowCount; i++)

{

if ((bool)gridView1.GetRowCellValue(i, gridView1.Columns["Check"]) == true)

{

count++;

}

}

return count;

}

private void CheckAll() // 选择全部

{

for (int i = 0; i < gridView1.DataRowCount; i++)

{

gridView1.SetRowCellValue(i, gridView1.Columns["Check"], true);

}

}

private void UnCheckAll() // 反选全部

{

for (int i = 0; i < gridView1.DataRowCount; i++)

{

gridView1.SetRowCellValue(i, gridView1.Columns["Check"], false);

}

}

private void CheckSingle() // 选择一个

{

gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["Check"], true);

}

private void UnCheckSingle() // 反选一个

{

gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["Check"], false);

}

#endregion

======================================================================

在窗体加载事件里的代码:

private void MainForm_Load(object sender, EventArgs e)

{

#region 从本地数据库中获取数据 并展示到gridView控件上

GridDataBind();

#endregion

#region 设置gridview列只读(不能编辑)

for (int i = 1; i < gridView1.Columns.Count; i++)

{

gridView1.Columns[i].OptionsColumn.ReadOnly = true;

}

#endregion

}

时间: 2024-11-06 09:39:04

Devexpress GridView增加CheckBox列的相关文章

讨论:C#Gridview增加超链接列

Q: 我想对Gridview的一个字段进行超链接,如title列,首先显示title列的内容,同时将其超链接到另一个页面 A1: 在绑定好gridview数据后,选择编辑列-->在字段选择HyperLinkField,选择添加然后在右边的属性NavigateUrl选择你要链接的网址.Target选择_blank空白窗口打开.你可以测试一下看看. A2: 添加列: <asp:GridView ID="GridView1" runat="server" Au

在DBGrid增加一列CheckBox(而非DBCheckBox)

自:http://rabbitfox.blog.sohu.com/33264033.html http://community.csdn.net/Expert/topic/5342/5342920.xml?temp=.9525568 问:如何使dbgrid增加一列类似checkbox的控件,可以用鼠标来选择该行是否被选中,可以多选 最好的思路还是要在数据表里增加一个布尔字段,然后用DBCheckBox与它关联,而不是CheckBox.(至于在DBGrid里嵌入DBCheckBox的方法,可以参考

DevExpress GridView 列标题点击事件

GridView有RowCellClick事件,即单元格点击事件,但是针对列标题行以及列标题单元格却没有相应的事件. 在这里使用GridView的MouseDown事件.这里同样使用的是GridHitInfo来获取点击位置的信息,来判断是否在列标题上.GridHitInfo根据鼠标点击的x.y坐标获取该点的相关信息,判断是否点击在列标题行内. private void gridView_MouseDown(object sender, MouseEventArgs e) { //鼠标左键点击 i

DevExpress gridView列标题右键菜单管理

1.添加PopupMenuShowing事件 2.PopupMenuShowing事件中的代码如下: private void gridView1_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e) { if (e.MenuType == DevExpress.XtraGrid.Views.Grid.GridMenuType.Column)//判断是否是列标题的右键

DevExpress GridView 那些事儿

1:去除 GridView 头上的 "Drag a column header here to group by that column" -->  点击 Run Designer  -> 找到:OptionView ->  将 ShowGroupPanel : 设置为 false ; 2:如何 显示出 GridView 自带的 搜索功能 -->  点击 Run Designer  ->  找到: OptionsFind -> 将AlwaysVisi

DevExpress Grid使用checkBox选中的方法

到官网得到消息自13.2版本后的Dev Grid中均内置了CheckBox列多选功能.在寻找答案的过程的成果进行记录. 一.13.2版本以后用法 启用多选列对Gird中的View进行以下属性设置: gridView1.OptionsSelection.MultiSelect = true; gridView1.OptionsSelection.MultiSelectMode = GridMultiSelectMode.CheckBoxRowSelect; 清除当前选择在选中列时后,可配置在选中列

DevExpress gridview 代码添加按钮

1.1 创建一个RepositoryItemButtonEdit方法 1 private RepositoryItemButtonEdit CreateRepositoryItemButtonEdit(Dictionary<object, string> dicButtons) 2 { 3 RepositoryItemButtonEdit repositoryBtn = new RepositoryItemButtonEdit(); 4 repositoryBtn.AppearanceDisa

[DevExpress]根据行,列索引来获取RepositoryItem

关键代码: /// <summary> /// 根据行,列索引来获取RepositoryItem /// </summary> /// <param name="view">GridView</param> /// <param name="rowIndex">行索引</param> /// <param name="columnIndex">列索引</pa

DevExpress gridview下拉框的再次研究

前几天写了一篇关于研究DevExpress gridview下拉框的随笔(DevExpress gridview下拉框repositoryItemComboBox的使用),被大神(@爱编程的大叔)评论为:成功用世界上最繁琐的方法来使用Devexpress中的Gridview控件中的下拉框,之后就一直在想那肯定是还有更加简便的办法,刚好今天有空闲时间,就去了官网研究起了gridControl的属性(https://documentation.devexpress.com/#WindowsForms