Browse Source

自定义全选列

章政 7 years ago
parent
commit
ff64ee7fff

+ 3 - 0
UAS_DeviceMonitor/CustomerControl/GridViewWithSerialNum/GridViewWithSerialNum.Designer.cs

@@ -32,7 +32,10 @@
             // 
             // GridViewWithSerialNum
             // 
+            this.CustomDrawColumnHeader += new DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventHandler(this.GridViewWithSerialNum_CustomDrawColumnHeader);
             this.CustomDrawRowIndicator += new DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventHandler(this.GridViewWithSerialNum_CustomDrawRowIndicator);
+            this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.GridViewWithSerialNum_MouseDown);
+            this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.GridViewWithSerialNum_MouseUp);
             ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
 
         }

+ 153 - 2
UAS_DeviceMonitor/CustomerControl/GridViewWithSerialNum/GridViewWithSerialNum.cs

@@ -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);
+            }
+        }
     }
-}
+}

+ 22 - 8
UAS_DeviceMonitor/Main.Designer.cs

@@ -68,11 +68,11 @@ namespace UAS_DeviceMonitor
             this.ButtonSaveCommand = new UAS_DeviceMonitor.CustomerControl.Button.ButtonSaveGrid();
             this.GridCommandSetting = new UAS_DeviceMonitor.CustomerControl.AutoDataGridControl.AutoDataGridControl();
             this.GridViewCommandSet = new UAS_DeviceMonitor.CustomerControl.GridViewWithSerialNum.GridViewWithSerialNum();
-            this.CheckEditCommandSet = new DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit();
             this.dc_id = new DevExpress.XtraGrid.Columns.GridColumn();
             this.dc_code = new DevExpress.XtraGrid.Columns.GridColumn();
             this.dc_name = new DevExpress.XtraGrid.Columns.GridColumn();
             this.dc_command = new DevExpress.XtraGrid.Columns.GridColumn();
+            this.CheckEditCommandSet = new DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit();
             this.PageDeviceKind = new DevExpress.XtraTab.XtraTabPage();
             this.PageDeviceStatus = new DevExpress.XtraTab.XtraTabPage();
             this.PagePollingSetting = new DevExpress.XtraTab.XtraTabPage();
@@ -83,6 +83,7 @@ namespace UAS_DeviceMonitor
             this.ButtonStartPolling = new DevExpress.XtraEditors.SimpleButton();
             this.GridPollingSetting = new UAS_DeviceMonitor.CustomerControl.AutoDataGridControl.AutoDataGridControl();
             this.GridViewPollSetting = new UAS_DeviceMonitor.CustomerControl.GridViewWithSerialNum.GridViewWithSerialNum();
+            this.COLUMNCHECKED = new DevExpress.XtraGrid.Columns.GridColumn();
             this.dpc_enableCheckEdit = new DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit();
             this.dpc_id = new DevExpress.XtraGrid.Columns.GridColumn();
             this.dpc_decode = new DevExpress.XtraGrid.Columns.GridColumn();
@@ -496,12 +497,6 @@ namespace UAS_DeviceMonitor
             this.GridViewCommandSet.OptionsSelection.MultiSelect = true;
             this.GridViewCommandSet.OptionsSelection.MultiSelectMode = DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.CheckBoxRowSelect;
             // 
-            // CheckEditCommandSet
-            // 
-            this.CheckEditCommandSet.AutoHeight = false;
-            this.CheckEditCommandSet.Name = "CheckEditCommandSet";
-            this.CheckEditCommandSet.NullStyle = DevExpress.XtraEditors.Controls.StyleIndeterminate.Unchecked;
-            // 
             // dc_id
             // 
             this.dc_id.FieldName = "DC_ID";
@@ -534,6 +529,12 @@ namespace UAS_DeviceMonitor
             this.dc_command.VisibleIndex = 3;
             this.dc_command.Width = 768;
             // 
+            // CheckEditCommandSet
+            // 
+            this.CheckEditCommandSet.AutoHeight = false;
+            this.CheckEditCommandSet.Name = "CheckEditCommandSet";
+            this.CheckEditCommandSet.NullStyle = DevExpress.XtraEditors.Controls.StyleIndeterminate.Unchecked;
+            // 
             // PageDeviceKind
             // 
             this.PageDeviceKind.Name = "PageDeviceKind";
@@ -639,6 +640,7 @@ namespace UAS_DeviceMonitor
             // GridViewPollSetting
             // 
             this.GridViewPollSetting.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] {
+            this.COLUMNCHECKED,
             this.dpc_id,
             this.dpc_decode,
             this.dpc_plcode,
@@ -653,10 +655,21 @@ namespace UAS_DeviceMonitor
             this.GridViewPollSetting.GridControl = this.GridPollingSetting;
             this.GridViewPollSetting.IndicatorWidth = 30;
             this.GridViewPollSetting.Name = "GridViewPollSetting";
-            this.GridViewPollSetting.OptionsSelection.MultiSelect = true;
             this.GridViewPollSetting.OptionsSelection.MultiSelectMode = DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.CheckBoxRowSelect;
             this.GridViewPollSetting.CellValueChanging += new DevExpress.XtraGrid.Views.Base.CellValueChangedEventHandler(this.GridViewPollSetting_CellValueChanging);
             // 
+            // COLUMNCHECKED
+            // 
+            this.COLUMNCHECKED.Caption = " ";
+            this.COLUMNCHECKED.ColumnEdit = this.dpc_enableCheckEdit;
+            this.COLUMNCHECKED.FieldName = "COLUMNCHECKED";
+            this.COLUMNCHECKED.Name = "COLUMNCHECKED";
+            this.COLUMNCHECKED.OptionsColumn.AllowSort = DevExpress.Utils.DefaultBoolean.False;
+            this.COLUMNCHECKED.OptionsFilter.AllowFilter = false;
+            this.COLUMNCHECKED.Tag = "0";
+            this.COLUMNCHECKED.Visible = true;
+            this.COLUMNCHECKED.VisibleIndex = 0;
+            // 
             // dpc_enableCheckEdit
             // 
             this.dpc_enableCheckEdit.AutoHeight = false;
@@ -1013,5 +1026,6 @@ namespace UAS_DeviceMonitor
         private CustomerControl.Button.ButtonSaveGrid ButtonSaveCommandSet;
         private DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit PollingSetItemLookUpEdit;
         private DevExpress.XtraGrid.Columns.GridColumn POLLSETTINGSTATUSCOLUMN;
+        private DevExpress.XtraGrid.Columns.GridColumn COLUMNCHECKED;
     }
 }

+ 2 - 2
UAS_DeviceMonitor/Main.cs

@@ -41,11 +41,11 @@ namespace UAS_DeviceMonitor
             ButtonSavePolling.Grid = GridPolling;
             ButtonAddPolling.Grid = GridPolling;
             //轮询配置界面
-            GridPollingSetting.GetDataSQL = "SELECT '' POLLSETTINGSTATUSCOLUMN,DPC_ID,DPC_DECODE ,DPC_PLCODE ,DPC_PLNAME ,DPC_INTERVAL ,DPC_DCCODE ,DPC_FUNCTION , DPC_ENABLE,DPC_STATUS ,DPC_REMARK FROM DEVICEPOLLINGCONFIG";
+            GridPollingSetting.GetDataSQL = "SELECT '' POLLSETTINGSTATUSCOLUMN,0 COLUMNCHECKED,DPC_ID,DPC_DECODE ,DPC_PLCODE ,DPC_PLNAME ,DPC_INTERVAL ,DPC_DCCODE ,DPC_FUNCTION , DPC_ENABLE,DPC_STATUS ,DPC_REMARK FROM DEVICEPOLLINGCONFIG";
             GridPollingSetting.ID = "DPC_ID";
             GridPollingSetting.TableName = "DEVICEPOLLINGCONFIG";
             GridPollingSetting.InsertSQL = "insert into DEVICEPOLLINGCONFIG(DPC_ID,DPC_DECODE ,DPC_PLCODE ,DPC_PLNAME ,DPC_INTERVAL ,DPC_DCCODE ,DPC_FUNCTION , DPC_ENABLE,DPC_STATUS ,DPC_REMARK) values(DEVICEPOLLINGCONFIG_seq.nextval,:DPC_DECODE ,:DPC_PLCODE ,:DPC_PLNAME ,:DPC_INTERVAL ,:DPC_DCCODE ,:DPC_FUNCTION , :DPC_ENABLE,:DPC_STATUS ,:DPC_REMARK)";
-        
+
             ButtonSaveCommandSet.Grid = GridPollingSetting;
             ButtonNewCommandSet.Grid = GridPollingSetting;
             ButtonDeleteCommandSet.Grid = GridPollingSetting;