0

我在 C# .net Windows 应用程序中有一个 datagridview ,其中有一个复选框列,并且我添加了一个标题复选框。

添加标题复选框和复选框列的代码是

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);
}

是否有可能如果选中所有复选框,则将选中标题复选框,并且在选中的复选框中未选中一个复选框,则在 C#.Net Windows 应用程序中未选中标题?

4

1 回答 1

0

尝试这个,

 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;
            }
        }
    }
于 2012-09-28T07:15:27.440 回答