【问题标题】:How to make my primary checkbox a default option for all the checking all other check boxes如何使我的主要复选框成为所有选中所有其他复选框的默认选项
【发布时间】:2014-10-28 15:40: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);
    }
}

【问题讨论】:

  • Stacy 我想我已经给出了这个问题的答案。有一个事件可用于编写代码以应用网格单元格值来检查或取消选中状态。将在标题复选框状态更改时调用的方法是OnCheckBoxClicked。在这里,您可以将所有网格单元格值应用于_checked 参数值循环。顺便说一句,我会在 1 小时后给你答复。
  • @Shell NO SHELL 你没有给我!!!我确定!!!关于那个

标签: c# winforms checkbox


【解决方案1】:

将数据源分配给网格后,您需要在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;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-25
    • 2016-09-03
    • 2021-12-01
    • 1970-01-01
    • 2013-01-31
    • 1970-01-01
    • 2013-04-28
    • 2014-04-29
    相关资源
    最近更新 更多