第一:添加列標題時,添加兩個空格——用於顯示復選框;
第二:實現列標題添加復選框,代碼如下:
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);
}
運行效果如下:

