zoukankan      html  css  js  c++  java
  • dataGridView1 checkbox全选

      private void AddHeaderCheckBox()
            {
                HeaderCheckBox = new CheckBox();
                HeaderCheckBox.Size = new Size(20, 20);
                dataGridView1.Controls.Add(HeaderCheckBox);
            }
     private void HeaderCheckBox_MouseClick(object sender, MouseEventArgs e)
            {
                HeaderCheckBoxClick((CheckBox)sender);
            }
    
            private void HeaderCheckBox_KeyUp(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Space)
                    HeaderCheckBoxClick((CheckBox)sender);
            }
    
            private void HeaderCheckBoxClick(CheckBox HCheckBox)
            {
                IsHeaderCheckBoxClicked = true;
               
              
                foreach (DataGridViewRow Row in dataGridView1.Rows)
                    ((DataGridViewCheckBoxCell)Row.Cells["selected"]).Value = HCheckBox.Checked;
                if (HCheckBox.Checked)
                {
                    foreach (var item in allPatientsId)
                    {
                        listPatientId.Add(item);
                    }
                }
                else
                {
                    listPatientId = new List<string>();
                }
                dataGridView1.RefreshEdit();
                Console.WriteLine("HeaderCheckBoxClick:");
                foreach (var item in listPatientId)
                {
                    Console.WriteLine(item);
                }
                TotalCheckedCheckBoxes = HCheckBox.Checked ? TotalCheckBoxes : 0;
    
                IsHeaderCheckBoxClicked = false;
            }
    
            private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
            {
                if (e.RowIndex == -1 && e.ColumnIndex == 0)
                    ResetHeaderCheckBoxLocation(e.ColumnIndex, e.RowIndex);
            }
    
    
            private void ResetHeaderCheckBoxLocation(int ColumnIndex, int RowIndex)
            {
                //Get the column header cell bounds
                Rectangle oRectangle = this.dataGridView1.GetCellDisplayRectangle(ColumnIndex, RowIndex, true);
    
                Point oPoint = new Point();
    
                oPoint.X = oRectangle.Location.X + (oRectangle.Width - HeaderCheckBox.Width) / 2 + 1;
                oPoint.Y = oRectangle.Location.Y + (oRectangle.Height - HeaderCheckBox.Height) / 2 + 1;
    
                //Change the location of the CheckBox to make it stay on the header
                HeaderCheckBox.Location = oPoint;
            }
  • 相关阅读:
    SP1716 GSS3
    A Simple Problem with Integers题解
    P4528 [CTSC2008]图腾 题解
    P1498 南蛮图腾 题解
    P2024 [NOI2001]食物链 题解
    Windows编程 Windows程序的生与死(中)
    Windows编程 Windows程序的生与死(上)
    C#实现在注册表中保存信息
    沿路径动画(Animation Along a Path)
    倾斜动画(SkewTransform)
  • 原文地址:https://www.cnblogs.com/liuxinls/p/2980891.html
Copyright © 2011-2022 走看看