8

我在 Winform 中有一个 dataGridView,我使用我在这里看到的代码向 datagrid 添加了一个带有复选框的列:

    DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
    {
        column.HeaderText = "Export";
        column.Name = "Export";
        column.AutoSizeMode =
            DataGridViewAutoSizeColumnMode.DisplayedCells;
        column.FlatStyle = FlatStyle.Standard;
        column.CellTemplate = new DataGridViewCheckBoxCell(false);
        column.CellTemplate.Style.BackColor = Color.White;
    }

    gStudyTable.Columns.Insert(0, column);  

这可行,但我希望将复选框检查为我添加的默认锯:

    foreach (DataGridViewRow row in gStudyTable.Rows)
    {                
        row.Cells[0].Value = true;
    }

但复选框 col 仍未选中。我使用集合作为我的数据源,并在添加数据源后更改 col 的值。

4

8 回答 8

12

我认为没有办法在列声明上设置检查值。在设置数据源后,您必须遍历检查它的行(例如在 DataBindingComplete 事件中):

for (int i = 0; i < dataGridView1.Rows.Count -1; i++)
{
dataGridView1.Rows[i].Cells[0].Value = true;
}

使用您的列名:

for (int i = 0; i < dataGridView1.Rows.Count -1; i++)
{
   dataGridView1.Rows[i].Cells["Export"].Value = true;
}
于 2012-12-10T07:10:32.167 回答
3

尝试这样做:

foreach (DataGridViewRow row in dgv.Rows)     
{
    row.Cells[CheckBoxColumn.Name].Value = true;     
} 
于 2012-12-10T07:19:31.217 回答
1

确保在设置 DataGridViewCheckBoxCells 的值时显示 DataGridView:我在 TabControl 的第二个选项卡中有我的,并且单元格在初始化后始终未选中。为了解决这个问题,我不得不将单元格初始化移动到 TabControl 的 SelectedIndexChanged 事件。

private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (this.tabControl1.SelectedIndex == 1)
    {
        foreach (DataGridViewRow row in this.myGridView.Rows)
        {
            ((DataGridViewCheckBoxCell)row.Cells[0]).Value = true;
        }
    }
}
于 2018-11-20T09:36:47.423 回答
0

if ((bool)this.dataGridView2.Rows[i].Cells[0].FormattedValue == true)

于 2013-07-05T17:22:40.347 回答
0

您可以使用数据表并在 datagridview 中创建列,然后添加行,为每行提供第一列值为“True”或“False”。

尝试这个

于 2013-05-22T11:20:51.643 回答
0

在加载网格中的值期间或之后,为了检查网格中的值,请使用此代码并设置网格列的属性

foreach (DataGridViewRow row in dataGridView.Rows)
{
    row.Cells[0].Value = 1;
}

在此处输入图像描述

于 2016-09-14T14:01:37.057 回答
0

查看 .xsd 文件中表格格式的属性:

表格行属性

然后确保将默认值设置为合理的值:

设置默认数据列属性值

然后它将自动将默认值设置为绑定源。

于 2021-11-14T22:42:40.273 回答
-2

一个简单的方法是使用NewRowIndex如下所示的属性:

dvgInvoiceItems.Rows[dvgInvoiceItems.NewRowIndex-1].Cells[5].Value = true;
dvgInvoiceItems.Rows[dvgInvoiceItems.NewRowIndex-1].Cells[6].Value = true;
于 2017-03-09T16:31:25.217 回答