GridViewWithSerialNum.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using DevExpress.XtraEditors;
  2. using DevExpress.XtraGrid.Columns;
  3. using DevExpress.XtraGrid.Views.Grid;
  4. using DevExpress.XtraGrid.Views.Grid.ViewInfo;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7. using System.Windows.Forms.VisualStyles;
  8. using UAS_PLCDataReader.Entity;
  9. namespace UAS_PLCDataReader.CustomerControl.GridViewWithSerialNum
  10. {
  11. public partial class GridViewWithSerialNum : GridView
  12. {
  13. private Rectangle checkBoxColumnHeaderRect = Rectangle.Empty;
  14. private GridColumn checkBoxColumn = null;
  15. public GridViewWithSerialNum()
  16. {
  17. InitializeComponent();
  18. IndicatorWidth = 30;
  19. }
  20. /// <summary>
  21. /// 绘制行号
  22. /// </summary>
  23. /// <param name="sender"></param>
  24. /// <param name="e"></param>
  25. private void GridViewWithSerialNum_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
  26. {
  27. if (e.Info.IsRowIndicator && e.RowHandle >= 0)
  28. {
  29. e.Info.DisplayText = (e.RowHandle + 1).ToString();
  30. }
  31. }
  32. /// <summary>
  33. /// 绘制指定的列头的CheckBox
  34. /// </summary>
  35. /// <param name="sender"></param>
  36. /// <param name="e"></param>
  37. private void GridViewWithSerialNum_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
  38. {
  39. if (e.Column != null && e.Column.FieldName == SystemInf.CheckedColumnName)
  40. {
  41. checkBoxColumnHeaderRect = e.Bounds;
  42. checkBoxColumn = e.Column;
  43. e.Column.Caption = " ";
  44. //须把列头标题设置为空
  45. e.Painter.DrawObject(e.Info);
  46. //在列头中心显示复选框
  47. int x = e.Bounds.X + (int)((e.Bounds.Width - CheckBoxRenderer.GetGlyphSize(e.Graphics, CheckBoxState.UncheckedNormal).Width) * 0.5);
  48. int y = e.Bounds.Y + (int)((e.Bounds.Height - CheckBoxRenderer.GetGlyphSize(e.Graphics, CheckBoxState.UncheckedNormal).Height) * 0.5);
  49. Point location = new Point(x, y);
  50. CheckBoxState checkBoxState;
  51. if (e.Column.Tag != null && e.Column.Tag.ToString() == "1")
  52. checkBoxState = CheckBoxState.CheckedPressed;
  53. else
  54. checkBoxState = CheckBoxState.UncheckedNormal;
  55. CheckBoxRenderer.DrawCheckBox(e.Graphics, location, checkBoxState);
  56. e.Handled = true;
  57. }
  58. }
  59. /// <summary>
  60. /// 执行全部勾选或者全部不勾选
  61. /// </summary>
  62. /// <param name="sender"></param>
  63. /// <param name="e"></param>
  64. private void GridViewWithSerialNum_MouseDown(object sender, MouseEventArgs e)
  65. {
  66. SyncCheckStatus(this, SystemInf.CheckedColumnName, e);
  67. }
  68. /// <summary>
  69. /// 绘制列头的CheckBox
  70. /// </summary>
  71. /// <param name="sender"></param>
  72. /// <param name="e"></param>
  73. private void GridViewWithSerialNum_MouseUp(object sender, MouseEventArgs e)
  74. {
  75. if (checkBoxColumnHeaderRect != Rectangle.Empty)
  76. {
  77. if (e.X > checkBoxColumnHeaderRect.X && e.X < (checkBoxColumnHeaderRect.X + checkBoxColumnHeaderRect.Width) && e.Y > checkBoxColumnHeaderRect.Y && e.Y < (checkBoxColumnHeaderRect.Y + checkBoxColumnHeaderRect.Height))
  78. {
  79. if (checkBoxColumn.Tag != null && checkBoxColumn.Tag.ToString() == "1" && CheckRowCount == RowCount)
  80. checkBoxColumn.Tag = "0";
  81. //在这写未全选逻辑
  82. else
  83. checkBoxColumn.Tag = "1";
  84. //在这写全选逻辑
  85. //InvalidateColumnHeader(checkBoxColumn);
  86. }
  87. }
  88. }
  89. /// <summary>
  90. /// GridViewWithSerialNum_MouseUp事件最后执行,提前记录勾选的行号,防止后面统计错误
  91. /// </summary>
  92. static int CheckRowCount;
  93. public static void SyncCheckStatus(GridView view, string fieldeName, MouseEventArgs e)
  94. {
  95. if (e.Button == MouseButtons.Left)
  96. {
  97. view.ClearSorting();
  98. view.PostEditor();
  99. GridHitInfo _info;
  100. Point _pt = view.GridControl.PointToClient(Control.MousePosition);
  101. _info = view.CalcHitInfo(_pt);
  102. if (_info.InColumn && _info.Column.FieldName.Equals(fieldeName))
  103. {
  104. CheckRowCount = getCheckedCount(view, fieldeName);
  105. if (CheckRowCount == view.DataRowCount)
  106. UnChekAll(view, fieldeName);
  107. else
  108. CheckAll(view, fieldeName);
  109. }
  110. }
  111. }
  112. /// <summary>
  113. /// 通过勾选的行数和总行数来判断下一次点击是否全部勾选
  114. /// </summary>
  115. /// <param name="view"></param>
  116. /// <param name="filedName"></param>
  117. /// <returns></returns>
  118. private static int getCheckedCount(GridView view, string filedName)
  119. {
  120. int count = 0;
  121. for (int i = 0; i < view.DataRowCount; i++)
  122. {
  123. object _cellValue = view.GetRowCellValue(i, view.Columns[filedName]);
  124. if (_cellValue == null) continue;
  125. if (string.IsNullOrEmpty(_cellValue.ToString().Trim())) continue;
  126. bool _checkStatus = false;
  127. if (bool.TryParse(_cellValue.ToString() == "0" ? "false" : "true", out _checkStatus))
  128. {
  129. if (_checkStatus)
  130. count++;
  131. }
  132. }
  133. return count;
  134. }
  135. /// <summary>
  136. /// 设置全选
  137. /// </summary>
  138. /// <param name="view"></param>
  139. /// <param name="fieldName"></param>
  140. private static void CheckAll(GridView view, string fieldName)
  141. {
  142. for (int i = 0; i < view.DataRowCount; i++)
  143. {
  144. view.SetRowCellValue(i, fieldName, true);
  145. }
  146. }
  147. /// <summary>
  148. /// 设置全部不选
  149. /// </summary>
  150. /// <param name="view"></param>
  151. /// <param name="fieldName"></param>
  152. private static void UnChekAll(GridView view, string fieldName)
  153. {
  154. for (int i = 0; i < view.DataRowCount; i++)
  155. {
  156. view.SetRowCellValue(i, fieldName, false);
  157. }
  158. }
  159. }
  160. }