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

有人可以帮助我为什么它不起作用? 我有一个checkbox ,如果我点击它,这应取消选中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); 

看看这个MSDN论坛发布它建议将Cell的值与Cell.TrueValue进行比较。

因此,通过它的示例,您的代码应该看起来像这样:( 这是完全未经测试的

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

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

当我看到这篇文章没有得到实际回答时,我正在制作我自己的Checkbox版本来控制DataGridViewCheckBoxColumn。 要设置DataGridViewCheckBoxCell的已检查状态,请使用:

 foreach (DataGridViewRow row in dataGridView1.Rows) { dataGridView1.Rows[row.Index].SetValues(true); } 

对于任何试图完成同样事情的人来说,这就是我想出的。

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

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

您在这里尝试的代码将翻转复选框的状态(如果为true,则反之亦然),而不管用户选择的复选框,因为foreach正在选择每个checkbox并执行操作。

为了清楚起见,在执行foreach操作之前存储所选用户index复选框,并在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 

上面的函数使所有复选框都为true,包括用户选择的CheckBox。 我想这就是你想要的……

简单的代码就可以了

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

以下代码是完美的

使用复选框CONTROL选择/取消选中数据网格上的复选框列

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

您可以在网格CellClick事件上使用此代码来检查或取消选中单元格复选框:

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

虽然所有其他答案都是正确的,但我会添加另一个对我有用的简单选项:

 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) 

只要cIndex指的是DataGridViewCheckBoxCell类型的单元格,这将正常工作。 希望这有助于somone。

你可以试试这段代码:

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

我遇到了同样的问题,即使这里提供的解决方案也没有用。 复选框根本不会改变,它们的值将保持为空。 我花了很多年才意识到自己的愚蠢:

事实certificate, 调用form1.Show() 之前,我在Form派生类Form1上调用了form1.PopulateDataGridView(my data) form1.Show() 。 当我更改顺序时,首先调用Show() ,然后读取数据并填入复选框,值不会保持为null。

如果在代码中创建单元格,则下面的代码允许用户取消/检查DataGridView中的复选框

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