DevExpress Grid使用checkBox选中的方法
到官网得到消息自13.2版本后的Dev Grid中均内置了CheckBox列多选功能。在寻找答案的过程的成果进行记录。
一、13.2版本以后用法
- 启用多选列
对Gird中的View进行以下属性设置:
gridView1.OptionsSelection.MultiSelect = true; gridView1.OptionsSelection.MultiSelectMode = GridMultiSelectMode.CheckBoxRowSelect;
- 清除当前选择
在选中列时后,可配置在选中列以外的地方点击时候会清除当前的选择。14以后才有此功能
gridView1.OptionsSelection.ResetSelectionClickOutsideCheckboxSelector = true; - 获取选中的行
二、早起版本实现
相关链接:http://www.devexpress.com/Support/Center/Example/Details/E1271
- GridCheckMarksSelection类
1 public class GridCheckMarksSelection { 2 protected GridView _view; 3 protected ArrayList selection; 4 GridColumn column; 5 RepositoryItemCheckEdit edit; 6 const int CheckboxIndent = 4; 7 8 public GridCheckMarksSelection() { 9 selection = new ArrayList(); 10 } 11 12 public GridCheckMarksSelection(GridView view) : this() { 13 View = view; 14 } 15 public GridView View { 16 get { return _view; } 17 set { 18 if (_view != value) { 19 Detach(); 20 Attach(value); 21 } 22 } 23 } 24 public GridColumn CheckMarkColumn { get { return column; } } 25 26 public int SelectedCount { get { return selection.Count; } } 27 public object GetSelectedRow(int index) { 28 return selection[index]; 29 } 30 public int GetSelectedIndex(object row) { 31 return selection.IndexOf(row); 32 } 33 public void ClearSelection() { 34 selection.Clear(); 35 Invalidate(); 36 } 37 public void SelectAll() { 38 selection.Clear(); 39 // fast (won't work if the grid is filtered) 40 //if(_view.DataSource is ICollection) 41 // selection.AddRange(((ICollection)_view.DataSource)); 42 //else 43 // slow: 44 for (int i = 0; i < _view.DataRowCount; i++) 45 selection.Add(_view.GetRow(i)); 46 Invalidate(); 47 } 48 public void SelectGroup(int rowHandle, bool select) { 49 if (IsGroupRowSelected(rowHandle) && select) return; 50 for (int i = 0; i < _view.GetChildRowCount(rowHandle); i++) { 51 int childRowHandle = _view.GetChildRowHandle(rowHandle, i); 52 if (_view.IsGroupRow(childRowHandle)) 53 SelectGroup(childRowHandle, select); 54 else 55 SelectRow(childRowHandle, select, false); 56 } 57 Invalidate(); 58 } 59 public void SelectRow(int rowHandle, bool select) { 60 SelectRow(rowHandle, select, true); 61 } 62 public void InvertRowSelection(int rowHandle) { 63 if (View.IsDataRow(rowHandle)) { 64 SelectRow(rowHandle, !IsRowSelected(rowHandle)); 65 } 66 if (View.IsGroupRow(rowHandle)) { 67 SelectGroup(rowHandle, !IsGroupRowSelected(rowHandle)); 68 } 69 } 70 public bool IsGroupRowSelected(int rowHandle) {
- GridCheckMarksSelection类