在winform下,gridview的checkbox列要实现全选和不全选,可以在该列的列头加个checkbox来实现吗?

解决方案 »

  1.   


    if (this.chkAllChecked.Checked)
                {
                    for (int count = 0; count < this.dgvCommon.Rows.Count; count++)
                    {
                        DataGridViewCheckBoxCell cbh = (DataGridViewCheckBoxCell)this.dgvCommon.Rows[count].Cells["chkClient"];
                        this.dgvCommon.Rows[count].Cells["chkClient"].Value = true;
                        this.dgvCommon.SelectAll();
                    }
                }
                else
                {
                    for (int count = 0; count < this.dgvCommon.Rows.Count; count++)
                    {
                        DataGridViewCheckBoxCell cbh = (DataGridViewCheckBoxCell)this.dgvCommon.Rows[count].Cells["chkClient"];
                        this.dgvCommon.Rows[count].Cells["chkClient"].Value = false;
                        this.dgvCommon.ClearSelection();
                    }
                }
    以下是行的全选代码,只需红色部分就可以
      

  2.   


            private void checkBoxAll_CheckedChanged(object sender, EventArgs e)
            {
                if (checkBoxAll.Checked)
                {
                    foreach (DataGridViewRow myDgvr in dataGridView1.Rows)
                    {
                        myDgvr.Cells["Xuan"].Value = 1;
                    }
                }
                else
                {
                    foreach (DataGridViewRow myDgvr in dataGridView1.Rows)
                    {
                        myDgvr.Cells["Xuan"].Value = 0;
                    }
                }
            }
      

  3.   

    错了:用下面这个.if (this.你要点击的checkbox.Checked)
                {
                    for (int count = 0; count < this.datagridview1.Rows.Count; count++)
                    {
                        DataGridViewCheckBoxCell cbh = (DataGridViewCheckBoxCell)this.dgvCommon.Rows[count].Cells["你的checkbox"];
                        this.datagridview1.Rows[count].Cells["你的checkbox"].Value = true;
                    }
                }
                else
                {
                    for (int count = 0; count < this.datagridview1.Rows.Count; count++)
                    {
                        DataGridViewCheckBoxCell cbh = (DataGridViewCheckBoxCell)this.dgvCommon.Rows[count].Cells["你的checkbox"];
                        this.datagridview1.Rows[count].Cells["你的checkbox"].Value = false;
                    }
                }
      

  4.   


    忘记说了,dataGridView1 中那个checkBox的TrueValue为1,FalseValue为0
      

  5.   

    我是想在checkbox的列头上也加个checkbox,就像网页上那样的效果,勾上列头的checkbox就全选,下面列中的,不够下面单元格的也都不选啊~