【问题标题】:Rows cannot be programmatically added to the datagridview's row collection when the control is data-bound当控件是数据绑定时,行不能以编程方式添加到 datagridview 的行集合中
【发布时间】:2012-02-01 05:51:39
【问题描述】:

首先,我在here 中查找了这个相关问题,但dataGridView1.Rows.Add() 的解决方案不适用于我的情况。

在我的 Datagridview 中,我有 3 个文本框用于数据输入和 2 个组合框供用户选择值(绑定到数据库中)。我的一个 TextBoxes 设置为只读,因此用户只能在数据网格之外填充它(使用普通的 TexBox 和一个按钮)。

当用户用数据填充 DataGridView 时,底部总是有一个空行;所以我禁用了这个,我用这个代码来防止用户在数据网格中添加一个新行......

dataGridView1.AllowUserToAddRows = false

我只想在用户单击我上面提到的按钮时添加一个新行(这会引发错误)。

我得到的错误信息是:

“当控件绑定数据时,不能以编程方式将行添加到 datagridview 的行集合中”

带红色箭头的是ComboBox,带绿色箭头的是只读TextBox

【问题讨论】:

  • 你在使用 .DataSource 属性吗?

标签: c# winforms datagridview


【解决方案1】:

看起来好像您正在使用 DataGridView 的 DataSource 属性。当此属性用于绑定数据时,您不能直接将行显式添加到 DataGridView。您必须改为将行直接添加到您的数据源。

例如,如果您的数据源是 DataTable,则使用分配给 DataSource 属性的 DataTable(未经测试):

private void AddARow(DataTable table)
{
    // Use the NewRow method to create a DataRow with 
    // the table's schema.
    DataRow newRow = table.NewRow();

    // Add the row to the rows collection.
    table.Rows.Add(newRow);
}

【讨论】:

  • 注意顶部也可以加:dt.Rows.InsertAt(row, 0);
【解决方案2】:

您可以获取DataGridViewDataSource 并将其转换为DataTable

然后添加一个新的DataRow 并设置字段的值。

将新行添加到 DataTable 并接受更改。

在 C# 中是这样的:

DataTable dataTable = (DataTable)dataGridView.DataSource;
DataRow drToAdd = dataTable.NewRow();

drToAdd["Field1"] = "Value1";
drToAdd["Field2"] = "Value2";

dataTable.Rows.Add(drToAdd);
dataTable.AcceptChanges();

【讨论】:

  • 如果您以另一种方法将DataTable 绑定到DataGridView 并且DataTable 不是公共变量,这是最好的解决方案。好主意!
  • 天哪,它解决了我的问题。我已经搜索了很长时间
【解决方案3】:

添加新行后,您必须在行数边界设置行索引。 您必须执行这些步骤。

  1. 首先,在DataGridView中添加行:

    dataGridView1.Rows.Add();
    
  2. 其次,设置新的行索引为count - 1:

    int RowIndex = dataGridView1.RowCount - 1;
    
  3. 最后,设置里面的控件值:

    DataGridViewRow R = dataGridView1.Rows[RowIndex];
    R.Cells["YourName"].Value = tbName.Text;
    

如果您的数据网格的源是数据表,您必须在该表中添加行。为数据表中新添加的行赋予新值,最后用更新的数据表重新绑定数据网格。

    DataRow row = dt.NewRow();  
    row["columnname"] = tbName.Text.toString();  
    dt.Rows.Add(row);
    dt.AcceptChanges();  

   dataGridView1.DataSource = dt;  
   dataGridView1.DataBind();

检查你是否正确设置了新行的索引。也许这就是您收到此错误的原因。

【讨论】:

  • 嗨,Syeda,刚才我尝试了你的方法。在调试模式下,进入第一步出现错误,即dataGridView1.Rows.Add();
  • 我为 gridview 和数据表提供了示例。您在使用数据表时尝试了为 gridview 提供的示例。
【解决方案4】:

Bound Datagridview 有一个问题,当您想以编程方式添加数据时,它会阻止它直接添加它。所以添加数据的间接和最好的方法是这样的..记住永远不要以编程方式将数据直接添加到datagridview,因为它总是会产生问题,而是将数据添加到您的数据源:-)

code for VB.NET
Dim r As DataRow ( C# : Datarow r=new Datarow() below codes apply to C# also)
r = dataset.Tables(0).NewRow
r.Item("field1") = "2"
r.Item("field2") = "somevalue"
dataset.Tables(0).Rows.Add(r)

dataset.Tables(0).acceptchanges()

the update will goes as you do ever

【讨论】:

    【解决方案5】:

    我找到的最佳解决方案:

    //create datatable and columns
    DataTable dtable = new DataTable();
    dtable.Columns.Add(new DataColumn("Column 1"));
    dtable.Columns.Add(new DataColumn("Column 2"));
    
    //simple way create object for rowvalues here i have given only 2 add as per your requirement
    object[] RowValues = { "", "" };
    
    //assign values into row object
    RowValues[0] = "your value 1";
    RowValues[1] = "your value 2";
    
    //create new data row
    DataRow dRow;
    dRow = dtable.Rows.Add(RowValues);
    dtable.AcceptChanges();
    
    //now bind datatable to gridview... 
    gridview.datasource=dtable;
    gridview.databind();
    

    来源:http://www.codeproject.com/Questions/615379/Adding-rows-to-datagridview-with-existing-columns

    【讨论】:

      【解决方案6】:

      当我在 Form_Load 中写下这一行时

      DGVData.DataSource = DataSQLite.GetData("SELECT * from tableName");
      

      并试图添加一个新行,这个错误出现在我身上

      不要使用 DataSource 试试这个代码

       private void Form_Load(object sender, EventArgs e) {
              DataSQLite.OpenDB();      // this line to open connection to Database and DataSQLite class i created it
              DataTable tbl = DataSQLite.GetData("SELECT * from tableName");
              for (int i = 0; i < tbl.Rows.Count; i++) {
                  DGVData.Rows.Add();  // this line will create new blank row
                  for (int j = 0; j < Numberofcolumns; j++) {
                      DGVData.Rows[i].Cells[j].Value = tbl.Rows[i][j].ToString();
                  }
              }
          }
      

      在此代码之后,您可以轻松添加行

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-01
        • 2014-03-02
        • 1970-01-01
        • 2012-01-10
        相关资源
        最近更新 更多