给GridView添加列头复选框
Posted albertsmith
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了给GridView添加列头复选框相关的知识,希望对你有一定的参考价值。
1 using DevExpress.XtraEditors.Drawing; 2 using DevExpress.XtraEditors.Repository; 3 using DevExpress.XtraEditors.ViewInfo; 4 using DevExpress.XtraGrid.Views.Grid; 5 using System; 6 using System.Collections.Generic; 7 using System.Drawing; 8 using System.Linq; 9 using System.Text; 10 11 public static class Draw 12 { 13 /// <summary> 14 /// 为列头绘制CheckBox 15 /// </summary> 16 /// <param name="view">GridView</param> 17 /// <param name="checkItem">RepositoryItemCheckEdit</param> 18 /// <param name="fieldName">需要绘制Checkbox的列名</param> 19 /// <param name="e">ColumnHeaderCustomDrawEventArgs</param> 20 public static void DrawHeaderCheckBox(this GridView view, RepositoryItemCheckEdit checkItem, string fieldName, ColumnHeaderCustomDrawEventArgs e) 21 { 22 /*说明: 23 *参考:https://www.devexpress.com/Support/Center/Question/Details/Q354489 24 *在CustomDrawColumnHeader中使用 25 *eg: 26 * private void gvCabChDetail_CustomDrawColumnHeader(object sender, DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e) 27 * { 28 * GridView _view = sender as GridView; 29 * _view.DrawHeaderCheckBox(CheckItem, "Check", e); 30 * } 31 */ 32 if (e.Column != null && e.Column.FieldName.Equals(fieldName)) 33 { 34 e.Info.InnerElements.Clear(); 35 e.Painter.DrawObject(e.Info); 36 DrawCheckBox(checkItem, e.Graphics, e.Bounds, getCheckedCount(view, fieldName) == view.DataRowCount); 37 e.Handled = true; 38 } 39 } 40 private static void DrawCheckBox(RepositoryItemCheckEdit checkItem, Graphics g, Rectangle r, bool Checked) 41 { 42 CheckEditViewInfo _info; 43 CheckEditPainter _painter; 44 ControlGraphicsInfoArgs _args; 45 _info = checkItem.CreateViewInfo() as CheckEditViewInfo; 46 _painter = checkItem.CreatePainter() as CheckEditPainter; 47 _info.EditValue = Checked; 48 49 _info.Bounds = r; 50 _info.PaintAppearance.ForeColor = Color.Black; 51 _info.CalcViewInfo(g); 52 _args = new ControlGraphicsInfoArgs(_info, new DevExpress.Utils.Drawing.GraphicsCache(g), r); 53 _painter.Draw(_args); 54 _args.Cache.Dispose(); 55 } 56 private static int getCheckedCount(GridView view, string filedName) 57 { 58 int count = 0; 59 for (int i = 0; i < view.DataRowCount; i++) 60 { 61 object _cellValue = view.GetRowCellValue(i, view.Columns[filedName]); 62 if (_cellValue == null) continue; 63 if (string.IsNullOrEmpty(_cellValue.ToString().Trim())) continue; 64 bool _checkStatus = false; 65 if (bool.TryParse(_cellValue.ToString(), out _checkStatus)) 66 { 67 if (_checkStatus) 68 count++; 69 } 70 } 71 return count; 72 } 73 }
使用方法:
1 RepositoryItemCheckEdit CheckItem = new RepositoryItemCheckEdit(); 2 const string gcCheckFieldName = "勾选"; 3 private void gvLampConfig_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e) 4 { 5 GridView _view = sender as GridView; 6 Draw.DrawHeaderCheckBox(gridView1, CheckItem, gcCheckFieldName, e); 7 }
以上是关于给GridView添加列头复选框的主要内容,如果未能解决你的问题,请参考以下文章
dev GridControl中的GridView怎么实现批量删除、修改GridView表中的任意数据后保存。求大神解决