void ch_OnCheckBoxClicked(object sender, datagridviewCheckboxHeaderEventArgs e) { foreach (DataGridViewRow dgvRow in dataGridView1.Rows) { if (e.CheckedState) { dgvRow.Cells[0].Value = true; } else { dgvRow.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) { _checked = !_checked; //获取列头checkbox的选择状态 datagridviewCheckboxHeaderEventArgs ex = new datagridviewCheckboxHeaderEventArgs(); ex.CheckedState = _checked; object sender = new object();//此处不代表选择的列头checkbox,只是作为参数传递。应该列头checkbox是绘制出来的,无法获得它的实例 if (OnCheckBoxClicked != null) { OnCheckBoxClicked(sender, ex);//触发单击事件 this.DataGridView.InvalidateCell(this); } } base.OnMouseClick(e); }