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