【问题标题】:DataGridView with a checkbox with default value checked带有选中默认值的复选框的 DataGridView
【发布时间】:2012-11-27 14:12:18
【问题描述】:

我在 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 的值。

【问题讨论】:

标签: c# winforms datagridview checkbox


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    我认为没有办法在列声明中设置检查值。 在设置数据源(例如在 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;
    }
    

    【讨论】:

    • 它适用于除最后一行之外的所有行。请将 for 循环更改为:for (int i = 0; i
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          【解决方案6】:

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

          【讨论】:

          • 您将来可能希望将这样的简短答案作为 cmets。
          【解决方案7】:

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

          试试这个

          【讨论】:

            【解决方案8】:

            尝试这样做:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2014-05-19
              • 1970-01-01
              • 2012-10-06
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-02-20
              相关资源
              最近更新 更多