如何使我的主复选框成为所有选中所有其他复选框的默认选项

本文关键字:复选框 其他 默认 选项 何使 我的 | 更新日期: 2023-09-27 18:27:58

我制作了一个windows窗体应用程序,在网格视图的标题中有一个复选框。我想使主标题复选框用于检查所有其他复选框。

所以就像我勾选主复选框一样https://imageshack.com/i/ippzf3rGp,应自动选中以下所有复选框。如果我取消选中主标题复选框,那么下面的所有复选框都应该取消选中。我如何做到这一点我的代码如下:

public delegate void CheckBoxClickedHandler(bool state);
public class DataGridViewCheckBoxHeaderCellEventArgs : EventArgs
{
    bool _bChecked;
    public DataGridViewCheckBoxHeaderCellEventArgs(bool bChecked)
    {
         _bChecked = bChecked;
    }
    public bool Checked
    {
        get { return _bChecked; }
    }
 }
 class DatagridViewCheckBoxHeaderCell : DataGridViewColumnHeaderCell
 {
     Point checkBoxLocation;
     Size checkBoxSize;
     bool _checked = false;
     Point _cellLocation = new Point();
     System.Windows.Forms.VisualStyles.CheckBoxState _cbState =
         System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;
     public event CheckBoxClickedHandler OnCheckBoxClicked;
     public DatagridViewCheckBoxHeaderCell()
     {
     }
     protected override void Paint(System.Drawing.Graphics graphics,
            System.Drawing.Rectangle clipBounds,
            System.Drawing.Rectangle cellBounds,
            int rowIndex,
            DataGridViewElementStates dataGridViewElementState,
            object value,
            object formattedValue,
            string errorText,
            DataGridViewCellStyle cellStyle,
            DataGridViewAdvancedBorderStyle advancedBorderStyle,
            DataGridViewPaintParts paintParts)
     {
         base.Paint(graphics, clipBounds, cellBounds, rowIndex,
                dataGridViewElementState, value,
                formattedValue, errorText, cellStyle,
                advancedBorderStyle, paintParts);
         Point p = new Point();
         Size s = CheckBoxRenderer.GetGlyphSize(graphics,
         System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);
         p.X = cellBounds.Location.X +
                (cellBounds.Width / 2) - (s.Width / 2);
         p.Y = cellBounds.Location.Y +
                (cellBounds.Height / 2) - (s.Height / 2);
         _cellLocation = cellBounds.Location;
         checkBoxLocation = p;
         checkBoxSize = s;
         if (_checked)
             _cbState = System.Windows.Forms.VisualStyles.
                    CheckBoxState.CheckedNormal;
         else
             _cbState = System.Windows.Forms.VisualStyles.
                    CheckBoxState.UncheckedNormal;
             CheckBoxRenderer.DrawCheckBox
                    (graphics, checkBoxLocation, _cbState);
         }
     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;
             if (OnCheckBoxClicked != null)
             {
                 OnCheckBoxClicked(_checked);
                 this.DataGridView.InvalidateCell(this);
             }
         }
         base.OnMouseClick(e);
    }
}

如何使我的主复选框成为所有选中所有其他复选框的默认选项

将数据源分配给网格后,需要在filter_table()方法上处理OnCheckBoxClicked事件。

private void filter_table()
{
    .... your code
    dataGridView1.DataSource = dt;
    cbHeader = (DatagridViewCheckBoxHeaderCell)dataGridView1.Columns[0].HeaderCell;
    cbHeader.OnCheckBoxClicked += new CheckBoxClickedHandler(cbHeader_OnCheckBoxClicked);
}

并在您的表单中添加以下方法(您也可以在filter_table()之后添加此方法)

private void cbHeader_OnCheckBoxClicked(bool _checked)
{
    for (int i = 0; i < dataGridView1.Rows.Count; i++)
    {
        //Give the check box column index instead of 0 in .Cells[0]
        dataGridView1.Rows[i].Cells[0].Value = _checked;
    }
}