|
|
@@ -1,15 +1,33 @@
|
|
|
-using DevExpress.XtraGrid.Views.Grid;
|
|
|
+using DevExpress.XtraEditors;
|
|
|
+using DevExpress.XtraGrid.Columns;
|
|
|
+using DevExpress.XtraGrid.Views.Grid;
|
|
|
+using DevExpress.XtraGrid.Views.Grid.ViewInfo;
|
|
|
+using System.Drawing;
|
|
|
+using System.Windows.Forms;
|
|
|
+using System.Windows.Forms.VisualStyles;
|
|
|
|
|
|
namespace UAS_DeviceMonitor.CustomerControl.GridViewWithSerialNum
|
|
|
{
|
|
|
public partial class GridViewWithSerialNum : GridView
|
|
|
{
|
|
|
+
|
|
|
+ string fieldName = "COLUMNCHECKED";
|
|
|
+
|
|
|
+ private Rectangle checkBoxColumnHeaderRect = Rectangle.Empty;
|
|
|
+
|
|
|
+ private GridColumn checkBoxColumn = null;
|
|
|
+
|
|
|
public GridViewWithSerialNum()
|
|
|
{
|
|
|
InitializeComponent();
|
|
|
IndicatorWidth = 30;
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 绘制行号
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="sender"></param>
|
|
|
+ /// <param name="e"></param>
|
|
|
private void GridViewWithSerialNum_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
|
|
|
{
|
|
|
if (e.Info.IsRowIndicator && e.RowHandle >= 0)
|
|
|
@@ -17,5 +35,138 @@ namespace UAS_DeviceMonitor.CustomerControl.GridViewWithSerialNum
|
|
|
e.Info.DisplayText = (e.RowHandle + 1).ToString();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 绘制指定的列头的CheckBox
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="sender"></param>
|
|
|
+ /// <param name="e"></param>
|
|
|
+ private void GridViewWithSerialNum_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
|
|
|
+ {
|
|
|
+ if (e.Column != null && e.Column.FieldName == fieldName)
|
|
|
+ {
|
|
|
+ checkBoxColumnHeaderRect = e.Bounds;
|
|
|
+ checkBoxColumn = e.Column;
|
|
|
+ e.Column.Caption = " ";
|
|
|
+ //须把列头标题设置为空
|
|
|
+ e.Painter.DrawObject(e.Info);
|
|
|
+ //在列头中心显示复选框
|
|
|
+ int x = e.Bounds.X + (int)((e.Bounds.Width - CheckBoxRenderer.GetGlyphSize(e.Graphics, CheckBoxState.UncheckedNormal).Width) * 0.5);
|
|
|
+ int y = e.Bounds.Y + (int)((e.Bounds.Height - CheckBoxRenderer.GetGlyphSize(e.Graphics, CheckBoxState.UncheckedNormal).Height) * 0.5);
|
|
|
+ Point location = new Point(x, y);
|
|
|
+ CheckBoxState checkBoxState;
|
|
|
+ if (e.Column.Tag != null && e.Column.Tag.ToString() == "1")
|
|
|
+ checkBoxState = CheckBoxState.CheckedPressed;
|
|
|
+ else
|
|
|
+ checkBoxState = CheckBoxState.UncheckedNormal;
|
|
|
+ CheckBoxRenderer.DrawCheckBox(e.Graphics, location, checkBoxState);
|
|
|
+ e.Handled = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 执行全部勾选或者全部不勾选
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="sender"></param>
|
|
|
+ /// <param name="e"></param>
|
|
|
+ private void GridViewWithSerialNum_MouseDown(object sender, MouseEventArgs e)
|
|
|
+ {
|
|
|
+ SyncCheckStatus(this, fieldName, e);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 绘制列头的CheckBox
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="sender"></param>
|
|
|
+ /// <param name="e"></param>
|
|
|
+ private void GridViewWithSerialNum_MouseUp(object sender, MouseEventArgs e)
|
|
|
+ {
|
|
|
+ if (checkBoxColumnHeaderRect != Rectangle.Empty)
|
|
|
+ {
|
|
|
+ if (e.X > checkBoxColumnHeaderRect.X && e.X < (checkBoxColumnHeaderRect.X + checkBoxColumnHeaderRect.Width) && e.Y > checkBoxColumnHeaderRect.Y && e.Y < (checkBoxColumnHeaderRect.Y + checkBoxColumnHeaderRect.Height))
|
|
|
+ {
|
|
|
+ if (checkBoxColumn.Tag != null && checkBoxColumn.Tag.ToString() == "1" && CheckRowCount == RowCount)
|
|
|
+ checkBoxColumn.Tag = "0";
|
|
|
+ //在这写未全选逻辑
|
|
|
+ else
|
|
|
+ checkBoxColumn.Tag = "1";
|
|
|
+ //在这写全选逻辑
|
|
|
+ //InvalidateColumnHeader(checkBoxColumn);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// GridViewWithSerialNum_MouseUp事件最后执行,提前记录勾选的行号,防止后面统计错误
|
|
|
+ /// </summary>
|
|
|
+ static int CheckRowCount;
|
|
|
+ public static void SyncCheckStatus(GridView view, string fieldeName, MouseEventArgs e)
|
|
|
+ {
|
|
|
+ if (e.Button == MouseButtons.Left)
|
|
|
+ {
|
|
|
+ view.ClearSorting();
|
|
|
+ view.PostEditor();
|
|
|
+ GridHitInfo _info;
|
|
|
+ Point _pt = view.GridControl.PointToClient(Control.MousePosition);
|
|
|
+ _info = view.CalcHitInfo(_pt);
|
|
|
+ if (_info.InColumn && _info.Column.FieldName.Equals(fieldeName))
|
|
|
+ {
|
|
|
+ CheckRowCount = getCheckedCount(view, fieldeName);
|
|
|
+ if (CheckRowCount == view.DataRowCount)
|
|
|
+ UnChekAll(view, fieldeName);
|
|
|
+ else
|
|
|
+ CheckAll(view, fieldeName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 通过勾选的行数和总行数来判断下一次点击是否全部勾选
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="view"></param>
|
|
|
+ /// <param name="filedName"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ private static int getCheckedCount(GridView view, string filedName)
|
|
|
+ {
|
|
|
+ int count = 0;
|
|
|
+ for (int i = 0; i < view.DataRowCount; i++)
|
|
|
+ {
|
|
|
+ object _cellValue = view.GetRowCellValue(i, view.Columns[filedName]);
|
|
|
+ if (_cellValue == null) continue;
|
|
|
+ if (string.IsNullOrEmpty(_cellValue.ToString().Trim())) continue;
|
|
|
+ bool _checkStatus = false;
|
|
|
+ if (bool.TryParse(_cellValue.ToString() == "0" ? "false" : "true", out _checkStatus))
|
|
|
+ {
|
|
|
+ if (_checkStatus)
|
|
|
+ count++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return count;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 设置全选
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="view"></param>
|
|
|
+ /// <param name="fieldName"></param>
|
|
|
+ private static void CheckAll(GridView view, string fieldName)
|
|
|
+ {
|
|
|
+ for (int i = 0; i < view.DataRowCount; i++)
|
|
|
+ {
|
|
|
+ view.SetRowCellValue(i, fieldName, true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 设置全部不选
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="view"></param>
|
|
|
+ /// <param name="fieldName"></param>
|
|
|
+ private static void UnChekAll(GridView view, string fieldName)
|
|
|
+ {
|
|
|
+ for (int i = 0; i < view.DataRowCount; i++)
|
|
|
+ {
|
|
|
+ view.SetRowCellValue(i, fieldName, false);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
-}
|
|
|
+}
|