Dev+Grid复选框(全选,反选)
DevExpress+Grid 全选反选记录
一、先建一个辅助类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | public class GridControlHelp { /// <summary> /// draw checkbox /// </summary> /// <param name="e"></param> /// <param name="chk"></param> public 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(); } } /// <summary> /// click checkbox event /// </summary> /// <param name="gridView"></param> /// <param name="fieldName"></param> /// <param name="currentStatus"></param> /// <returns></returns> 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; 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控件的几个事件
gvBk_Click控件click事件
gvBk_CustomDrawColumnHeader表头重绘事件
gvBk_DataSourceChanged数据源改变事件
gvBk_RowCellClick单元格点击事件
bool checkStatus = true;默认是否选中状态
1.数据源中添加选中状态字段Bool类型,True选中,false取消选中
1 2 3 4 5 6 | DataTable table = EdiBookingBiz.GetBookingBySchid(ssd, user); //用vessel和voyage去找booking if (table.Rows.Count > 0) { table.Columns.Add( "Chk" , System.Type.GetType( "System.Boolean" )); table.Columns[ "Chk" ].DefaultValue = Boolean.FalseString; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | bool checkStatus = true ; private void gvBk_Click( object sender, EventArgs e) { if (GridControlHelp.ClickGridCheckBox(gvBk, "Chk" , checkStatus)) { checkStatus = !checkStatus; } } private void gvBk_CustomDrawColumnHeader( object sender, ColumnHeaderCustomDrawEventArgs e) { if (e.Column != null && e.Column.FieldName == "Chk" ) { e.Info.InnerElements.Clear(); e.Painter.DrawObject(e.Info); GridControlHelp.DrawCheckBox(e, checkStatus); e.Handled = true ; } } private void gvBk_DataSourceChanged( object sender, EventArgs e) { GridColumn column = this .gvBk.Columns.ColumnByFieldName( "Chk" ); if (column != null ) { column.Width = 80; column.OptionsColumn.ShowCaption = false ; column.ColumnEdit = new RepositoryItemCheckEdit(); } } private void gvBk_RowCellClick( object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e) { string columnName = gvBk.FocusedColumn.FieldName.ToString(); if (columnName == "Chk" ) { DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit repositoryCheck = e.Column.ColumnEdit as DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit; string value = gvBk.GetFocusedRowCellValue(columnName).ToString(); int hand = e.RowHandle; if (value == "True" ) { gvBk.SetRowCellValue(hand, "Chk" , false ); } else { gvBk.SetRowCellValue(hand, "Chk" , true ); } //修改后的值更新到数据源 gvBk.CloseEditor(); gvBk.UpdateCurrentRow(); } } //默认全选的方法 private void SelectAll(DataTable tableSource) { if (!checkStatus) checkStatus = true ; if (tableSource != null && tableSource.Rows.Count > 0) { foreach (DataRow item in tableSource.Rows) { item[ "Chk" ] = true ; } gridBk.DataSource = tableSource; } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥