C# 带有选中默认值的复选框的 DataGridView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13796300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:49:20  来源:igfitidea点击:

DataGridView with a checkbox with default value checked

c#winformsdatagridviewcheckbox

提问by meirrav

I have a dataGridView in a Winform, I added to the datagrid a column with a checkbox using a code I saw here :

我在 Winform 中有一个 dataGridView,我使用我在此处看到的代码向数据网格添加了一个带有复选框的列:

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

this works but I want the checkBox to be checked as a default saw I added :

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

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

but the checkbox col is still unchecked. I'm using a collection as my data source and I change the value of the col after I added the data source.

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

采纳答案by Carlos Landeras

I think there is no way of setting the checked value on column declaration. You will have to irerate rows checking it after datasource is set (for example in DataBindingComplete event):

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

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

With your column name:

使用您的列名:

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

回答by Sylca

Try to do it like this:

尝试这样做:

foreach (DataGridViewRow row in dgv.Rows)     
{
    row.Cells[CheckBoxColumn.Name].Value = true;     
} 

回答by Degoldeni

You can use datatable and create columns in datagridview and then add rows, provided first column value as 'True' or 'False' for each row.

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

try this

尝试这个

回答by nadeera

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

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

回答by daniele3004

During or after load the value in a grid, for check the value in grid use this code and set the properties of grid column

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

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

enter image description here

在此处输入图片说明

回答by user5229434

An easy way is to use the property NewRowIndexas shown below:

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

dvgInvoiceItems.Rows[dvgInvoiceItems.NewRowIndex-1].Cells[5].Value = true;
dvgInvoiceItems.Rows[dvgInvoiceItems.NewRowIndex-1].Cells[6].Value = true;

回答by Sue Maurizio

Make sure your DataGridView is shown when you set the value of your DataGridViewCheckBoxCells: I had mine in the second tab of a TabControl and the cells were always unchecked after initialization. To solve this, I had to move cell initialization to TabControl's SelectedIndexChanged event.

确保在设置 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;
        }
    }
}