第一:添加列标题时,添加两个空格——用于显示复选框;
第二:实现列标题添加复选框,代码如下:
private void AddCheckeBoxToDGVHeader(DataGridView dgv) { for (int i = 0; i < this.dgvList.Columns.Count; i++) { System.Windows.Forms.CheckBox ckBox = new System.Windows.Forms.CheckBox(); //ckBox.Text = "全选"; ckBox.Checked = true; System.Drawing.Rectangle rect = dgv.GetCellDisplayRectangle(i, -1, false); ckBox.Size = new System.Drawing.Size(25, 25); ckBox.Location = rect.Location; ckBox.Padding = new System.Windows.Forms.Padding(2, 6, 0, 0); ckBox.BackColor = Color.Transparent; ckBox.Name = dgv.Columns[i].Name; ckBox.CheckedChanged += new EventHandler(ckBox_CheckedChanged); dgv.Controls.Add(ckBox); } } void ckBox_CheckedChanged(object sender, EventArgs e) { CheckBox chb = sender as CheckBox; MessageBox.Show("Test=="+ chb.Name); }
运行效果如下:
原文地址:https://www.cnblogs.com/YYkun/p/9186949.html
时间: 2024-11-08 19:39:39