DataGridView HeadCell 添加 CheckBox

添加CheckBox

#region headCell 添加 CheckBox
        public static CheckBox ckBox = new System.Windows.Forms.CheckBox();
        public static System.Windows.Forms.DataGridView dgv;
        public static void AddFullSelect()
        {
            ckBox.Text = "colsCk";
            ckBox.Checked = false;
            System.Drawing.Rectangle rect = dgv.GetCellDisplayRectangle(0, -1, true);
            ckBox.Size = new System.Drawing.Size(13, 13);
            ckBox.Location = new Point(rect.Location.X + dgv.Columns[0].Width / 2 - 13 / 2 - 1, rect.Location.Y + 3);
            //ckBox.Location.Offset(-40, rect.Location.Y);
            ckBox.CheckedChanged += new EventHandler(ckBox_CheckedChanged);
            dgv.Controls.Add(ckBox);
        }
        static void ckBox_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < dgv.Rows.Count; i++)
            {
                dgv.Rows[i].Cells["colselect"].Value = ((System.Windows.Forms.CheckBox)sender).Checked;
            }
            dgv.EndEdit();
        }

        #endregion

窗体加载事件

 dgv = dgvResult;
            AddFullSelect();

DataGridView HeadCell 添加 CheckBox_第1张图片

你可能感兴趣的:(资料记录,DataGridView,CheckBox,HeadCell)