C# 选中/取消选中datagridview上的复选框

C# 选中/取消选中datagridview上的复选框,c#,winforms,datagridview,datagridviewcheckboxcell,C#,Winforms,Datagridview,Datagridviewcheckboxcell,有人能帮我解释一下为什么它不起作用吗? 我有一个复选框,如果我点击它, 这应该取消选中datagridview中的所有复选框,这些复选框是在包含用户选中的复选框之前选中的 代码如下: private void chkItems_CheckedChanged(object sender, EventArgs e) { foreach (DataGridViewRow row in datagridview1.Rows)

有人能帮我解释一下为什么它不起作用吗? 我有一个
复选框
,如果我点击它, 这应该取消选中datagridview中的所有复选框,这些复选框是在包含用户选中的复选框之前选中的

代码如下:

        private void chkItems_CheckedChanged(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in datagridview1.Rows)
            {
                DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[1];
                if (chk.Selected == true)
                {
                    chk.Selected = false;
                }
                else
                {
                    chk.Selected = true;
                }
            }
        }
不应选中该复选框。应该检查一下

这是添加的列

            DataGridViewCheckBoxColumn CheckboxColumn = new DataGridViewCheckBoxColumn();
            CheckBox chk = new CheckBox();
            CheckboxColumn.Width = 20;
            datagridview1.Columns.Add(CheckboxColumn);

您在此处尝试的代码将翻转复选框的状态(如果为true,则变为false,反之亦然),而不考虑用户选择的复选框,因为此处的
foreach
选择每个
复选框并执行操作

为了清楚起见,在执行
foreach
操作之前和
foreach
操作之后,存储用户选择的复选框的
索引
,通过提及存储的索引调用该复选框并选中它(在您的情况下,使其为
True
——我认为)

这只是逻辑,我敢肯定这是正确的。如果可能,我将尝试实现一些示例代码

修改您的
foreach
如下:

    //Store the index of the selected checkbox here as Integer (you can use e.RowIndex or e.ColumnIndex for it).
    private void chkItems_CheckedChanged(object sender, EventArgs e)
    {
        foreach (DataGridViewRow row in datagridview1.Rows)
        {
            DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[1];
            if (chk.Selected == true)
            {
                chk.Selected = false;
            }
            else
            {
                chk.Selected = true;
            }
        }
    }
    //write the function for checking(making true) the user selected checkbox by calling the stored Index
上述函数使所有复选框均为真,包括用户选中的复选框。我想这就是你想要的。

看看这个,它建议将单元格的值与

因此,根据其示例,您的代码应该如下所示:(这是完全未经测试的)

编辑:未绑定DataGridViewCheckBox的Cell.TrueValue的默认值似乎为空。您需要在列定义中设置它

private void chkItems_CheckedChanged(object sender, EventArgs e)
{
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[1];
        if (chk.Value  == chk.TrueValue)
        {
            chk.Value = chk.FalseValue;
        }
        else
        {
            chk.Value = chk.TrueValue;
        }
    }
}

此代码是工作说明,在构造函数中设置TrueValue和FalseValue,并检查null:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        DataGridViewCheckBoxColumn CheckboxColumn = new DataGridViewCheckBoxColumn();
        CheckboxColumn.TrueValue = true;
        CheckboxColumn.FalseValue = false;
        CheckboxColumn.Width = 100;
        dataGridView1.Columns.Add(CheckboxColumn);
        dataGridView1.Rows.Add(4);
    }

    private void chkItems_CheckedChanged(object sender, EventArgs e)
    {
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];
            if (chk.Value == chk.FalseValue || chk.Value == null)
            {
                chk.Value = chk.TrueValue;
            }
            else
            {
                chk.Value = chk.FalseValue;
            }

        }
        dataGridView1.EndEdit();
    }
}

我正在制作自己版本的复选框来控制DataGridViewCheckBoxColumn,这时我看到这篇文章实际上没有得到回复。要设置DataGridViewCheckBoxCell的选中状态,请使用:

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    dataGridView1.Rows[row.Index].SetValues(true);
}
对于其他试图完成同样任务的人来说,以下是我的想法

这使得这两个控件的行为类似于Gmail中的复选框列。它保留了鼠标和键盘的功能

using System;
using System.Windows.Forms;

namespace Check_UnCheck_All
{
    public partial class Check_UnCheck_All : Form
    {
        public Check_UnCheck_All()
        {
            InitializeComponent();
            dataGridView1.RowCount = 10;
            dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvApps_CellContentClick);
            this.dataGridView1.CellMouseUp += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.myDataGrid_OnCellMouseUp);
            this.dataGridView1.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.myDataGrid_OnCellValueChanged);
            this.checkBox1.Click += new System.EventHandler(this.checkBox1_Click);
        }

        public int chkInt = 0;
        public bool chked = false;

        public void myDataGrid_OnCellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == dataGridView1.Rows[0].Index && e.RowIndex != -1)
            {
                DataGridViewCheckBoxCell chk = dataGridView1.Rows[e.RowIndex].Cells[0] as DataGridViewCheckBoxCell;

                if (Convert.ToBoolean(chk.Value) == true) chkInt++;
                if (Convert.ToBoolean(chk.Value) == false) chkInt--;
                if (chkInt < dataGridView1.Rows.Count && chkInt > 0)
                {
                    checkBox1.CheckState = CheckState.Indeterminate;
                    chked = true;
                }
                else if (chkInt == 0)
                {
                    checkBox1.CheckState = CheckState.Unchecked;
                    chked = false;
                }
                else if (chkInt == dataGridView1.Rows.Count)
                {
                    checkBox1.CheckState = CheckState.Checked;
                    chked = true;
                }
            }
        }
        public void myDataGrid_OnCellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            // End of edition on each click on column of checkbox
            if (e.ColumnIndex == dataGridView1.Rows[0].Index && e.RowIndex != -1)
            {
                dataGridView1.EndEdit();
            }
            dataGridView1.BeginEdit(true);
        }
        public void dgvApps_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.CurrentCell.GetType() == typeof(DataGridViewCheckBoxCell))
            {
                if (dataGridView1.CurrentCell.IsInEditMode)
                {
                    if (dataGridView1.IsCurrentCellDirty)
                    {
                        dataGridView1.EndEdit();
                    }
                }
                dataGridView1.BeginEdit(true);
            }
        }
        public void checkBox1_Click(object sender, EventArgs e)
        {
            if (chked == true)
            {
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];
                    if (chk.Value == chk.TrueValue)
                    {
                        chk.Value = chk.FalseValue;
                    }
                    else
                    {
                        chk.Value = chk.TrueValue;
                    }
                }
                chked = false;
                chkInt = 0;
                return;
            }
            if (chked == false)
            {
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    dataGridView1.Rows[row.Index].SetValues(true);
                }
                chked = true;
                chkInt = dataGridView1.Rows.Count;
            }
        }
    }
}
使用系统;
使用System.Windows.Forms;
名称空间选中\取消选中\全部
{
公共部分类选中\u取消选中\u全部:表单
{
公共检查(全部取消检查)
{
初始化组件();
dataGridView1.RowCount=10;
dataGridView1.AllowUserToAddress=false;
this.dataGridView1.CellContentClick+=new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvApps\u CellContentClick);
this.dataGridView1.CellMouseUp+=新系统.Windows.Forms.DataGridViewCellMouseEventHandler(this.myDataGrid_OnCellMouseUp);
this.dataGridView1.CellValueChanged+=new System.Windows.Forms.DataGridViewCellEventHandler(this.myDataGrid\u OnCellValueChanged);
this.checkBox1.Click+=new System.EventHandler(this.checkBox1\u Click);
}
公共int chkInt=0;
公共bool chked=false;
public void myDataGrid_OnCellValueChanged(对象发送方,DataGridViewCellEventArgs e)
{
如果(e.ColumnIndex==dataGridView1.Rows[0]。索引和&e.RowIndex!=-1)
{
DataGridViewCheckBoxCell chk=dataGridView1.Rows[e.RowIndex]。单元格[0]作为DataGridViewCheckBoxCell;
if(Convert.ToBoolean(chk.Value)==true)chkInt++;
如果(Convert.ToBoolean(chk.Value)==false)chkInt--;
如果(chkInt0)
{
checkBox1.CheckState=CheckState.Undeterminate;
chked=true;
}
else if(chkInt==0)
{
checkBox1.CheckState=CheckState.Unchecked;
chked=假;
}
else if(chkInt==dataGridView1.Rows.Count)
{
checkBox1.CheckState=CheckState.Checked;
chked=true;
}
}
}
public void myDataGrid_OnCellMouseUp(对象发送方,DataGridViewCellMouseEventArgs e)
{
//每次单击复选框的列时,版本结束
如果(e.ColumnIndex==dataGridView1.Rows[0]。索引和&e.RowIndex!=-1)
{
dataGridView1.EndEdit();
}
dataGridView1.BeginEdit(true);
}
public void dgvApps_CellContentClick(对象发送者,DataGridViewCellEventArgs e)
{
if(dataGridView1.CurrentCell.GetType()==typeof(DataGridViewCheckBoxCell))
{
if(dataGridView1.CurrentCell.IsInEditMode)
{
if(dataGridView1.IsCurrentCellDirty)
{
dataGridView1.EndEdit();
}
}
dataGridView1.BeginEdit(true);
}
}
公共无效复选框1\u单击(对象发送者,事件参数e)
{
如果(chked==true)
{
foreach(dataGridView1.Rows中的DataGridViewRow行)
{
DataGridViewCheckBoxCell chk=(DataGridViewCheckBoxCell)行。单元格[0];
如果(chk.Value==chk.TrueValue)
{
chk.Value=chk.false值;
}
其他的
{
chk.Value=chk.TrueValue;
}
}
chked=假;
chkInt=0;
返回;
}
如果(chked==false)
{
foreach(dataGridView1.Rows中的DataGridViewRow行)
{
dataGridView1.Rows[row.Index].SetValues(true);
}
chked=true;
chkInt=dataGridView1.Rows.Count;
}
}
}
}
var r = dataGridView.Rows[rIndex];
var c = r.Cells[cIndex];
var value = (bool) c.Value; 
c.Value = !value;
var r = dataGridView.Rows[rIndex];
r.Cells[cIndex].Value = !((bool) r.Cells[cIndex].Value)
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dataGridView1.CurrentRow.Cells[0];
                dataGridView1.BeginEdit(true);
                if (chk.Value == null || (int)chk.Value == 0)
                {
                    chk.Value = 1;
                }
                else
                {
                    chk.Value = 0;
                }
                dataGridView1.EndEdit();
    private void checkBox2_CheckedChanged(object sender, EventArgs e)
    {
        if (checkBox2.Checked == false)
        {
            foreach (DataGridViewRow row in dGV1.Rows)
            {
                DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];

                    chk.Value = chk.TrueValue;
            }
       }
       else if(checkBox2.Checked==true)
       {
            foreach (DataGridViewRow row in dGV1.Rows)
            {
                DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];
                chk.Value = 1;
                if (row.IsNewRow)
                {
                    chk.Value = 0;
                }
            }
        }
    }
private void checkBox2_CheckedChanged(object sender, EventArgs e) 
{
    if (checkBox2.Checked == false)
    {
        foreach (DataGridViewRow row in dGV1.Rows)
        {
            DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];
            chk.Value = chk.TrueValue;
        }
    }
   else if (checkBox2.Checked == true)
    {
        foreach (DataGridViewRow row in dGV1.Rows)
        {
            DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];
            chk.Value = 1;
            if (row.IsNewRow)
            {
                chk.Value = 0;
            }
        }
    }
}
// here is a simple way to do so

//irate through the gridview
            foreach (DataGridViewRow row in PifGrid.Rows)
            {
//store the cell (which is checkbox cell) in an object
                DataGridViewCheckBoxCell oCell = row.Cells["Check"] as DataGridViewCheckBoxCell;

//check if the checkbox is checked or not
                bool bChecked = (null != oCell && null != oCell.Value && true == (bool)oCell.Value);

//if its checked then uncheck it other wise check it
                if (!bChecked)
                {
                    row.Cells["Check"].Value = true;

                }
                else
                {
                    row.Cells["Check"].Value = false;

                }
            }
private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (dgv.CurrentRow.Cells["ColumnNumber"].Value != null && (bool)dgv.CurrentRow.Cells["ColumnNumber"].Value)
    {
        dgv.CurrentRow.Cells["ColumnNumber"].Value = false;
        dgv.CurrentRow.Cells["ColumnNumber"].Value = null;
    }
    else if (dgv.CurrentRow.Cells["ColumnNumber"].Value == null )
    {
        dgv.CurrentRow.Cells["ColumnNumber"].Value = true;
    }
}
private void gvData_CellClick(object sender, DataGridViewCellEventArgs e)
{
    DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)gvData.Rows[e.RowIndex].Cells[0];

    if (chk.Value == chk.TrueValue)
    {
        gvData.Rows[e.RowIndex].Cells[0].Value = chk.FalseValue;
    }
    else
    {
        gvData.Rows[e.RowIndex].Cells[0].Value = chk.TrueValue;
    }

}
private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == dataGridView.Columns["Select"].Index)
        {
            dataGridView.EndEdit();

            if ((bool)dataGridView.Rows[e.RowIndex].Cells["Select"].Value)
            {
                //-- checking current select, needs to uncheck any other cells that are checked
                foreach(DataGridViewRow row in dataGridView.Rows)
                {
                    if (row.Index == e.RowIndex)
                    {
                        dataGridView.Rows[row.Index].SetValues(true);
                    }
                    else
                    {
                        dataGridView.Rows[row.Index].SetValues(false);
                    }
                }
            }
        }
    }
private void Grid_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 0)
        {
            Grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = (Grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null ? true : (!(bool)Grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value));
        }
    }
  foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if (row.Cells[0].Value != null && (bool)row.Cells[0].Value)
            {
                Console.WriteLine(row.Cells[0].Value);
            }

        }
if (e.ColumnIndex == datagridview.Columns["columncheckbox"].Index)
dgvChkBxCell = datagridview.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewCheckBoxCell;
if ((bool)dgvChkBxCell.EditingCellFormattedValue) { }
 if (dgvDetails.RowCount > 0)
                    {
                        dgvDetails.ClearSelection(); 
                        dgvDetails.BeginEdit(true); 
                      
                        foreach (DataGridViewRow dgvr in dgvDetails.Rows)
                        {
                            dgvr.Cells["cellName"].Value = true;
                        }
                        dgvDetails.EndEdit();
                    }
private void Grid_CellClick(object sender, DataGridViewCellEventArgs e)
{

    if(Convert.ToBoolean(this.Grid.Rows[e.RowIndex].Cells["Selected"].Value) == false)
    {
        this.Grid.Rows[e.RowIndex].Cells["Selected"].Value = true;
    }
    else
    {
        this.productSpecGrid.Rows[e.RowIndex].Cells["Selected"].Value = false;
    }
}