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;
            }
  • 相关阅读:
    java编程思想概括(第二章)一切都是对象
    java编程思想概括(第六章)复用类
    .net程序在无.net环境下运行
    一个让人哭笑不得的触发器
    iReaper for android
    博易博客删除垃圾评论
    解决博易2.0版分页问题
    允许更新此预编译站点的作用
    java包的命名规则技巧
    简易拨号器iCall
  • 原文地址:https://www.cnblogs.com/liuxinls/p/2980891.html
Copyright © 2011-2022 走看看