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

发布于 2024-10-12 07:30:42 字数 341 浏览 6 评论 0原文

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

我可以使用该复选框获得正确的值

(bool)row.Cells[CheckBoxColumn.Index].FormattedValue

,但这只是一个吸气剂。

我尝试使用设置单元格的值

(bool)row.Cells[CheckBoxColumn.Index].value = false

,但这不会影响 FormattedValue。

我该如何解决这个问题?

How can I programmatically uncheck all rows in a DataGridViewCheckboxColumn in a datagridview?

I can get the correct value of the checkbox using

(bool)row.Cells[CheckBoxColumn.Index].FormattedValue

but that's only a getter.

I have tried setting the value of the cell using

(bool)row.Cells[CheckBoxColumn.Index].value = false

but that doesn't affect the FormattedValue.

How can I solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

怀里藏娇 2024-10-19 07:30:42

你做某事。就像:

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

您只是忘记转换为正确的类型,通用的DataGridViewCell不知道它的值类型。

You do sth. like:

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

You just forgot to cast to the correct type, a generic DataGridViewCell doesn't know its value-type.

南城追梦 2024-10-19 07:30:42

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

尝试到这种程度。

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

Have you tried casting the first control in the checkbox column to checkbox and then setting 'Checked' to true?

Try something to this extent.

((DataGridViewCheckBoxCell)e.Rows[0].Cells[0]).Selected = true
笑红尘 2024-10-19 07:30:42

如果您仅使用 dataGridView1_ContextClick 来执行“false”datagidviewCheckBox 列,则需要此代码:

dataGridView1.CancelEdit();

但如果您需要 DataGrid 的 CheckBoxColumns 的所有行:

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

If you use dataGridView1_ContextClick just for do "false" datagidviewCheckBox Column need this Code :

dataGridView1.CancelEdit();

but if you need all rows of CheckBoxColumns of DataGrid :

private void button1_Click(object sender, EventArgs e)
{
    foreach (DataGridViewRow r in dataGridView1.Rows)
    {
        r.Cells["statusBox"].Value = true;
    }
}
余生共白头 2024-10-19 07:30:42

您应该在检查后使用 YourDataGridview.EndEdit()

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

you should just use YourDataGridview.EndEdit() after checking.

(row.Cells[CheckBoxColumn.Index] as DataGridViewCheckBoxCell).value = false;
YourDataGridview.EndEdit();
握住我的手 2024-10-19 07:30:42

还没有检查过,但你可以尝试;

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

它的类型可能不同。只需调试并将其转换为原来的样子即可。

Haven't checked but you can try;

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

It's type may be different. Just debug and cast it to what it is.

迷你仙 2024-10-19 07:30:42
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
  dr.Cells[0].Value = true;//sıfırın
}
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
  dr.Cells[0].Value = true;//sıfırın
}
愚人国度 2024-10-19 07:30:42

取决于您想要执行的操作

如果是关于所选行,那么您可以:

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

无论适合您什么。继续努力吧!

Depending on what you wish to do

If it is about the selected row, then you can:

DataGridViewRow row = dataGridViewName.CurrentRow;

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

However if you wish to do it for the whole DataGridView table then:

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

Whatever suites you. Keep it up!

回忆那么伤 2024-10-19 07:30:42

循环遍历网格视图的每一行并使用 find 控件方法:

foreach ( GridViewRow row in myGridView )
{
     CheckBox checkBox = ( CheckBox ) row.FindControl( "myCheckBox" );
     checkbox.Checked = false;
}

Loop through each row of grid view and use the find control method:

foreach ( GridViewRow row in myGridView )
{
     CheckBox checkBox = ( CheckBox ) row.FindControl( "myCheckBox" );
     checkbox.Checked = false;
}
孤独岁月 2024-10-19 07:30:42
 foreach (DataGridViewRow row in datagridviewname.Rows)

  {
    row.Cells[CheckBoxColumn1_Name].Value = false;
  }
 foreach (DataGridViewRow row in datagridviewname.Rows)

  {
    row.Cells[CheckBoxColumn1_Name].Value = false;
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文