[英]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.