C# 以编程方式取消选中datagridview中的checkboxcolumn

C# 以编程方式取消选中datagridview中的checkboxcolumn,c#,winforms,datagridview,datagridviewcheckboxcell,C#,Winforms,Datagridview,Datagridviewcheckboxcell,如何以编程方式取消选中datagridview中DataGridViewCheckboxColumn中的所有行 我可以使用 (bool)row.Cells[CheckBoxColumn.Index].FormattedValue 但这只是一种激励 我已尝试使用 (bool)row.Cells[CheckBoxColumn.Index].value = false 但这并不影响FormattedValue 如何解决此问题?您是否尝试过将复选框列中的第一个控件强制转换为checkbox,然后将“

如何以编程方式取消选中datagridview中DataGridViewCheckboxColumn中的所有行

我可以使用

(bool)row.Cells[CheckBoxColumn.Index].FormattedValue
但这只是一种激励

我已尝试使用

(bool)row.Cells[CheckBoxColumn.Index].value = false
但这并不影响FormattedValue


如何解决此问题?

您是否尝试过将复选框列中的第一个控件强制转换为checkbox,然后将“Checked”设置为true

试着做一些这样的事情

((DataGridViewCheckBoxCell)e.Rows[0].Cells[0]).Selected = true

尚未检查,但您可以尝试

CheckBox cb = (row.Cells[CheckBoxColumn.Index].Controls[0] as CheckBox);
if(cb != null)
{
    cb.Checked = false;
}

它的类型可能不同。只需调试并将其转换为当前状态。

循环遍历网格视图的每一行,并使用“查找控制”方法:

foreach ( GridViewRow row in myGridView )
{
     CheckBox checkBox = ( CheckBox ) row.FindControl( "myCheckBox" );
     checkbox.Checked = false;
}
你做某事,比如:

(row.Cells[CheckBoxColumn.Index] as DataGridViewCheckBoxCell).value = false;

您只是忘记强制转换到正确的类型,一个泛型的
DataGridViewCell
不知道它的值类型。

如果您使用dataGridView1\u上下文单击仅用于执行“false”datagidviewCheckBox列需要此代码:

foreach (DataGridViewRow dr in dataGridView1.Rows)
{
  dr.Cells[0].Value = true;//sıfırın
}
dataGridView1.CancelEdit();
但如果您需要DataGrid的所有CheckBoxColumns行:

private void button1_Click(object sender, EventArgs e)
{
    foreach (DataGridViewRow r in dataGridView1.Rows)
    {
        r.Cells["statusBox"].Value = true;
    }
}

取决于你想做什么

如果是关于所选行的,则可以:

DataGridViewRow row = dataGridViewName.CurrentRow;

//This will assign the opposite value of the Cell Content
row.Cells["ColumnName"].Value = !Convert.ToBoolean(row.Cells["ColumnName"].Value);
但是,如果希望对整个DataGridView表执行,则:

    foreach (DataGridViewRow row in dataGridViewName.Rows)

   {
        //This will assign the opposite value of the Cell Content
        row.Cells["ColumnName"].Value = !Convert.ToBoolean(row.Cells["ColumnName"].Value);
   }

你喜欢什么都行。坚持下去

也许是代码片段?你说的“第一控制”是什么意思?行,单元格…?datagridviewrow没有.Controls[]。datagridviewcell没有.Controls[]也许FindControl只是用于web的东西?我正在开发一个WinForms应用程序。这种方式很有效。您的代码不会影响FormattedValue,我使用它来获取复选框的值。我们可以把问题扭转过来,把责任推到价值上。你看,当我试图从(row.Cells[CheckBoxColumn.Index]作为DataGridViewCheckBoxCell.value)获取布尔值时,应用程序崩溃了。这使得我有必要坚持使用FormattedValue。实际上,我使用它将值设置为true,但它不起作用。当您在此处发布内容时,请始终将代码(或至少注释)翻译为英语
    foreach (DataGridViewRow row in dataGridViewName.Rows)

   {
        //This will assign the opposite value of the Cell Content
        row.Cells["ColumnName"].Value = !Convert.ToBoolean(row.Cells["ColumnName"].Value);
   }