123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- 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;
- using UAS_PLCDataReader.Entity;
- namespace UAS_PLCDataReader.CustomerControl.GridViewWithSerialNum
- {
- public partial class GridViewWithSerialNum : GridView
- {
- 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)
- {
- 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 == SystemInf.CheckedColumnName)
- {
- 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, SystemInf.CheckedColumnName, 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);
- }
- }
- }
- }
|