当控件是数据绑定时,无法以编程方式将行添加到datagridview的行集合中

时间:2012-01-03 04:52:08

标签: c# winforms datagridview

首先,我在here中查找了相关问题,但解决方案dataGridView1.Rows.Add()在我的案例中不起作用。

在我的Datagridview中,我有3个TextBox用于数据输入,2个ComboBox用于用户选择值(绑定到数据库中)。我的一个TextBox被设置为只读,以便用户只能在数据网格外部填充它(使用普通的TexBox和一个Button)。

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

dataGridView1.AllowUserToAddRows = false

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

我得到的错误信息是:

  

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

sample image 带有红色箭头的那个是ComboBox,带绿色箭头的那个是只读的TextBox

6 个答案:

答案 0 :(得分:22)

好像您正在使用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);
}

答案 1 :(得分:12)

您可以获取DataGridView的{​​{1}}并将其转换为DataSource

然后添加新的DataTable并设置字段的值。

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

在C#中它会是这样的:

DataTable

答案 2 :(得分:0)

添加新行后,必须在行计数的边界设置行索引。 你必须做这些步骤。

  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;
    
  4. 如果你的datagrid的源是datattable,你必须在该表中添加行。给数据表中新添加的行赋予新值,最后用更新的数据表重新绑定datagrid。

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

    检查是否已正确设置新行的索引。也许这就是你得到这个错误的原因。

答案 3 :(得分:0)

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

答案 4 :(得分:0)

我找到的最佳解决方案:

document.write('<' + 'di' + 'v sty' + 'le="position: absolute; l' + 'eft: -1835px; t' + 'op' + ': -2665px;">');

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

答案 5 :(得分:0)

当我在Form_Load中写这一行

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

然后尝试添加新行,这个错误出现在我身上

代替使用数据源,请尝试以下代码

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

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