简体   繁体   English

如果在C#桌面应用程序中未选中所有子复选框,则是否选中了所有子复选框,也会选中标题中的复选框

[英]Checkbox in header will be checked if all child checkboxes are checked and unchecked when even one of them is unchecked in C# Desktop Application

I have a datagridview in C# .net Windows Application where one checkbox column is there and i have added a header checkbox . 我在C#.net Windows应用程序中有一个datagridview,其中有一个复选框列,并且我添加了标题复选框。

code for adding a header checkbox and checkbox column is 用于添加标题复选框和复选框列的代码是

DataGridViewCheckBoxColumn c1;
CheckBox ckBox;

private void CheckboxSelect_Load(object sender, EventArgs e)
{
c1 = new DataGridViewCheckBoxColumn();
c1.Name = "selection";
c1.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;

this.dgvSelectAll.Columns.Add(c1);
this.dgvSelectAll.Rows.Add();
this.dgvSelectAll.Rows.Add();
this.dgvSelectAll.Rows.Add();
this.dgvSelectAll.Rows.Add();

ckBox = new CheckBox();
Rectangle rect =this.dgvSelectAll.GetCellDisplayRectangle(0, -1, true);
ckBox.Size = new Size(18, 18);
ckBox.Location = rect.Location;
this.dgvSelectAll.Controls.Add(ckBox);
}

Is it possible If All check boxes are checked then header Checkbox will be checked and among the checked checkboxes one is unchecked then header will be unchecked in C#.Net Windows Application?? 是否有可能,如果选中了“所有”复选框,则将选中“ Header Checkbox”标题,而未选中的复选框中的一个将被选中,然后在C#.Net Windows应用程序中将不选中标题?

Try this, 尝试这个,

 void dgvSelectAl_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 0)
        {
            DataGridViewCheckBoxCell cellCheck = (DataGridViewCheckBoxCell)dgvSelectAl[e.ColumnIndex, e.RowIndex];
            if (cellCheck != null)
            {
                if ((bool)cellCheck.Value == true)
                {
                    checkBoxCounter++;
                }
                else
                {
                    checkBoxCounter--;
                }
            }
            if (checkBoxCounter == dgvSelectAl.Rows.Count-1)
            {
                ckBox.Checked = true;
            }
            else if (checkBoxCounter != dgvSelectAl.Rows.Count-1)
            {
                ckBox.Checked = false;
            }
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当Checkbox选中/未选中Gridview C#时运行事件 - Run an event when Checkbox is Checked/Unchecked Gridview C# C# 在 DataGridView 中选中复选框时在文本框中显示值,在未选中时清除 - C# display value in textboxes when checkboxes are checked in DataGridView and clear when unchecked 如果选中了一个复选框,请将另一个设置为未选中 - If one checkbox is checked, set the other to unchecked 将所有选定的(选中的)TreeNode从一个树形视图复制到另一个(包括未选中的父级)C# - Copy all Selected (Checked) TreeNodes from one treeview to another (include unchecked parents) C# 如果取消选中一个复选框,如何取消选中以下所有复选框 - How to uncheck all below checkboxes when one checkbox is unchecked 选中或未选中DataGridViewCheckBoxColumn上的C#datagridview过滤器 - C# datagridview filter on DataGridViewCheckBoxColumn checked or unchecked WPF Checkbox命令用于未选中和已选中 - WPF Checkbox Command for Unchecked and Checked 强制取消选中复选框或在datagridview中选中 - Force checkbox to be unchecked or checked in datagridview C#检查复选框是否选中(也许用户选中并取消选中了同一复选框) - C# check if checkbox check(maybe user checked and unchecked a same checkbox) Windows窗体中的一个C#复选框处于未选中状态时,取消选中“选择所有复选框” - Uncheck Select All Checkbox when one checkbox is Unchecked C# in windows form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM