GridCheckMarksSelection.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. using DevExpress.Utils.Drawing;
  2. using DevExpress.XtraEditors;
  3. using DevExpress.XtraEditors.Repository;
  4. using DevExpress.XtraGrid.Columns;
  5. using DevExpress.XtraGrid.Views.Base;
  6. using DevExpress.XtraGrid.Views.Grid;
  7. using DevExpress.XtraGrid.Views.Grid.ViewInfo;
  8. using System;
  9. using System.Collections;
  10. using System.Collections.Generic;
  11. using System.Drawing;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows.Forms;
  16. namespace ProjectBase.Controls
  17. {
  18. /// <summary>
  19. /// GridView选择变化的委托定义
  20. /// </summary>
  21. public delegate void SelectionChangedEventHandler(object sender, EventArgs e);
  22. /// <summary>
  23. /// GridView复选框选择的辅助类
  24. /// </summary>
  25. public class GridCheckMarksSelection
  26. {
  27. protected GridView _view;
  28. protected ArrayList selection;
  29. GridColumn column;
  30. RepositoryItemCheckEdit edit;
  31. const int CheckboxIndent = 4;
  32. GridLookUpEdit _gridLookUpEdit;
  33. /// <summary>
  34. /// 以GridView为参数的构造函数
  35. /// </summary>
  36. /// <param name="view">GridView</param>
  37. public GridCheckMarksSelection(GridView view) : this()
  38. {
  39. this.View = view;
  40. }
  41. /// <summary>
  42. /// 以GridLookUpEdit为参数的构造函数
  43. /// </summary>
  44. /// <param name="control"></param>
  45. public GridCheckMarksSelection(GridLookUpEdit control) : this()
  46. {
  47. this.View = control.Properties.View;
  48. this._gridLookUpEdit = control;
  49. }
  50. /// <summary>
  51. /// 表格视图对象
  52. /// </summary>
  53. public GridView View
  54. {
  55. get { return _view; }
  56. set
  57. {
  58. if (_view != value)
  59. {
  60. Detach();
  61. Attach(value);
  62. }
  63. }
  64. }
  65. public GridColumn CheckMarkColumn
  66. {
  67. get { return column; }
  68. }
  69. public GridLookUpEdit GridCheckLookUpEdit
  70. {
  71. get { return _gridLookUpEdit; }
  72. }
  73. public GridCheckMarksSelection()
  74. {
  75. selection = new ArrayList();
  76. this.OnSelectionChanged();
  77. }
  78. public ArrayList Selection
  79. {
  80. get { return selection; }
  81. set { selection = value; }
  82. }
  83. public int SelectedCount
  84. {
  85. get { return selection.Count; }
  86. }
  87. public object GetSelectedRow(int index)
  88. {
  89. return selection[index];
  90. }
  91. public int GetSelectedIndex(object row)
  92. {
  93. return selection.IndexOf(row);
  94. }
  95. public void ClearSelection()
  96. {
  97. selection.Clear();
  98. Invalidate();
  99. OnSelectionChanged();
  100. }
  101. public void SelectAll()
  102. {
  103. SelectAll(null);
  104. }
  105. public void SelectAll(object dataSource)
  106. {
  107. selection.Clear();
  108. if (dataSource != null)
  109. {
  110. if (dataSource is ICollection)
  111. selection.AddRange(((ICollection)dataSource));
  112. }
  113. else
  114. {
  115. for (int i = 0; i < _view.DataRowCount; i++)
  116. selection.Add(_view.GetRow(i));
  117. }
  118. Invalidate();
  119. this.OnSelectionChanged();
  120. }
  121. /// <summary>
  122. /// 处理选择变化的事件
  123. /// </summary>
  124. public event SelectionChangedEventHandler SelectionChanged;
  125. /// <summary>
  126. /// 选择变化事件的处理
  127. /// </summary>
  128. public void OnSelectionChanged()
  129. {
  130. if (SelectionChanged != null)
  131. {
  132. EventArgs e = new EventArgs();
  133. SelectionChanged(this, e);
  134. }
  135. }
  136. public void SelectGroup(int rowHandle, bool select)
  137. {
  138. if (IsGroupRowSelected(rowHandle) && select) return;
  139. for (int i = 0; i < _view.GetChildRowCount(rowHandle); i++)
  140. {
  141. int childRowHandle = _view.GetChildRowHandle(rowHandle, i);
  142. if (_view.IsGroupRow(childRowHandle))
  143. SelectGroup(childRowHandle, select);
  144. else
  145. SelectRow(childRowHandle, select, false);
  146. }
  147. Invalidate();
  148. }
  149. public void SelectRow(int rowHandle, bool select)
  150. { SelectRow(rowHandle, select, true); }
  151. public void InvertRowSelection(int rowHandle)
  152. {
  153. if (View.IsDataRow(rowHandle))
  154. SelectRow(rowHandle, !IsRowSelected(rowHandle));
  155. if (View.IsGroupRow(rowHandle))
  156. SelectGroup(rowHandle, !IsGroupRowSelected(rowHandle));
  157. }
  158. public bool IsGroupRowSelected(int rowHandle)
  159. {
  160. for (int i = 0; i < _view.GetChildRowCount(rowHandle); i++)
  161. {
  162. int row = _view.GetChildRowHandle(rowHandle, i);
  163. if (_view.IsGroupRow(row))
  164. {
  165. if (!IsGroupRowSelected(row)) return false;
  166. }
  167. else
  168. if (!IsRowSelected(row)) return false;
  169. }
  170. return true;
  171. }
  172. /// <summary>
  173. /// 检查指定的行是否选中
  174. /// </summary>
  175. /// <param name="rowHandle"></param>
  176. /// <returns></returns>
  177. public bool IsRowSelected(int rowHandle)
  178. {
  179. if (_view.IsGroupRow(rowHandle))
  180. return IsGroupRowSelected(rowHandle);
  181. object row = _view.GetRow(rowHandle);
  182. return GetSelectedIndex(row) != -1;
  183. }
  184. protected virtual void Attach(GridView view)
  185. {
  186. if (view == null) return;
  187. selection.Clear();
  188. this._view = view;
  189. view.BeginUpdate();
  190. try
  191. {
  192. edit = view.GridControl.RepositoryItems.Add("CheckEdit") as RepositoryItemCheckEdit;
  193. column = view.Columns.Add();
  194. column.OptionsColumn.AllowSort = DevExpress.Utils.DefaultBoolean.False;
  195. column.Visible = true;
  196. column.VisibleIndex = 0;
  197. column.FieldName = "CheckMarkSelection";
  198. column.Caption = "Mark";
  199. column.OptionsColumn.ShowCaption = false;
  200. column.OptionsColumn.AllowEdit = false;
  201. column.OptionsColumn.AllowSize = false;
  202. column.UnboundType = DevExpress.Data.UnboundColumnType.Boolean;
  203. column.Width = GetCheckBoxWidth();
  204. column.ColumnEdit = edit;
  205. view.Click += new EventHandler(View_Click);
  206. view.CustomDrawColumnHeader += new ColumnHeaderCustomDrawEventHandler(View_CustomDrawColumnHeader);
  207. view.CustomDrawGroupRow += new RowObjectCustomDrawEventHandler(View_CustomDrawGroupRow);
  208. view.CustomUnboundColumnData += new CustomColumnDataEventHandler(view_CustomUnboundColumnData);
  209. view.KeyDown += new KeyEventHandler(view_KeyDown);
  210. }
  211. finally
  212. {
  213. view.EndUpdate();
  214. }
  215. }
  216. protected virtual void Detach()
  217. {
  218. if (_view == null) return;
  219. if (column != null)
  220. column.Dispose();
  221. if (edit != null)
  222. {
  223. _view.GridControl.RepositoryItems.Remove(edit);
  224. edit.Dispose();
  225. }
  226. _view.Click -= new EventHandler(View_Click);
  227. _view.CustomDrawColumnHeader -= new ColumnHeaderCustomDrawEventHandler(View_CustomDrawColumnHeader);
  228. _view.CustomDrawGroupRow -= new RowObjectCustomDrawEventHandler(View_CustomDrawGroupRow);
  229. _view.CustomUnboundColumnData -= new CustomColumnDataEventHandler(view_CustomUnboundColumnData);
  230. _view.KeyDown -= new KeyEventHandler(view_KeyDown);
  231. _view = null;
  232. }
  233. protected int GetCheckBoxWidth()
  234. {
  235. DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo info = edit.CreateViewInfo() as DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo;
  236. int width = 0;
  237. GraphicsInfo.Default.AddGraphics(null);
  238. try
  239. {
  240. width = info.CalcBestFit(GraphicsInfo.Default.Graphics).Width;
  241. }
  242. finally
  243. {
  244. GraphicsInfo.Default.ReleaseGraphics();
  245. }
  246. return width + CheckboxIndent * 2;
  247. }
  248. protected void DrawCheckBox(Graphics g, Rectangle r, bool Checked)
  249. {
  250. DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo info;
  251. DevExpress.XtraEditors.Drawing.CheckEditPainter painter;
  252. DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs args;
  253. info = edit.CreateViewInfo() as DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo;
  254. painter = edit.CreatePainter() as DevExpress.XtraEditors.Drawing.CheckEditPainter;
  255. info.EditValue = Checked;
  256. info.Bounds = r;
  257. info.CalcViewInfo(g);
  258. args = new DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs(info, new DevExpress.Utils.Drawing.GraphicsCache(g), r);
  259. painter.Draw(args);
  260. args.Cache.Dispose();
  261. }
  262. void Invalidate()
  263. {
  264. _view.BeginUpdate();
  265. _view.EndUpdate();
  266. }
  267. void SelectRow(int rowHandle, bool select, bool invalidate)
  268. {
  269. if (IsRowSelected(rowHandle) == select) return;
  270. object row = _view.GetRow(rowHandle);
  271. if (select)
  272. selection.Add(row);
  273. else
  274. selection.Remove(row);
  275. if (invalidate)
  276. Invalidate();
  277. OnSelectionChanged();
  278. }
  279. void view_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e)
  280. {
  281. if (e.Column == CheckMarkColumn)
  282. {
  283. if (e.IsGetData)
  284. e.Value = IsRowSelected(View.GetRowHandle(e.ListSourceRowIndex));
  285. else
  286. SelectRow(View.GetRowHandle(e.ListSourceRowIndex), (bool)e.Value);
  287. }
  288. }
  289. void view_KeyDown(object sender, KeyEventArgs e)
  290. {
  291. if (View.FocusedColumn != column || e.KeyCode != Keys.Space) return;
  292. InvertRowSelection(View.FocusedRowHandle);
  293. }
  294. void View_Click(object sender, EventArgs e)
  295. {
  296. GridHitInfo info;
  297. Point pt = _view.GridControl.PointToClient(Control.MousePosition);
  298. info = _view.CalcHitInfo(pt);
  299. if (info.Column == column)
  300. {
  301. if (info.InColumn)
  302. {
  303. if (SelectedCount == _view.DataRowCount)
  304. ClearSelection();
  305. else
  306. SelectAll();
  307. }
  308. if (info.InRowCell)
  309. InvertRowSelection(info.RowHandle);
  310. }
  311. if (info.InRow && _view.IsGroupRow(info.RowHandle) && info.HitTest != GridHitTest.RowGroupButton)
  312. InvertRowSelection(info.RowHandle);
  313. }
  314. void View_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
  315. {
  316. if (e.Column == column)
  317. {
  318. e.Info.InnerElements.Clear();
  319. e.Painter.DrawObject(e.Info);
  320. DrawCheckBox(e.Graphics, e.Bounds, SelectedCount == _view.DataRowCount);
  321. e.Handled = true;
  322. }
  323. }
  324. void View_CustomDrawGroupRow(object sender, RowObjectCustomDrawEventArgs e)
  325. {
  326. DevExpress.XtraGrid.Views.Grid.ViewInfo.GridGroupRowInfo info;
  327. info = e.Info as DevExpress.XtraGrid.Views.Grid.ViewInfo.GridGroupRowInfo;
  328. info.GroupText = " " + info.GroupText.TrimStart();
  329. e.Info.Paint.FillRectangle(e.Graphics, e.Appearance.GetBackBrush(e.Cache), e.Bounds);
  330. e.Painter.DrawObject(e.Info);
  331. Rectangle r = info.ButtonBounds;
  332. r.Offset(r.Width + CheckboxIndent * 2 - 1, 0);
  333. DrawCheckBox(e.Graphics, r, IsGroupRowSelected(e.RowHandle));
  334. e.Handled = true;
  335. }
  336. }
  337. }