void cell_OnCheckBox2Clicked(object sender, DataGridviewCheckboxHeaderEventHander e) { foreach (DataGridViewRow row in this.dataGridView1.Rows) { if (e.CheckedState) { row.Cells[0].Value = true; } else { row.Cells[0].Value = false; } } }
/// <summary> /// 响应点击列头checkbox单击事件 /// </summary> protected override void OnMouseClick(DataGridViewCellMouseEventArgs e) { Point p = new Point(e.X + cellLocation.X, e.Y + cellLocation.Y); if (p.X >= checkBoxLocation.X && p.X <= checkBoxLocation.X + checkBoxSize.Width && p.Y >= checkBoxLocation.Y && p.Y <= checkBoxLocation.Y + checkBoxSize.Height) { isChecked = !isChecked; //获取列头checkbox的选择状态 DataGridviewCheckboxHeaderEventHander ex = new DataGridviewCheckboxHeaderEventHander(); ex.CheckedState = isChecked; //此处不代表选择的列头checkbox,只是作为参数传递。应该列头checkbox是绘制出来的,无法获得它的实例 object sender = new object(); if (OnCheckBoxClicked != null) { //触发单击事件 OnCheckBoxClicked(sender, ex); this.DataGridView.InvalidateCell(this); } } base.OnMouseClick(e); }