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

C# 当控件为数据绑定时,无法以编程方式将行添加到datagridview的行集合,c#,winforms,datagridview,C#,Winforms,Datagridview,首先,我在中查找了这个相关问题,但解决方案dataGridView1.Rows.Add在我的案例中不起作用 在我的Datagridview中,我有3个用于数据输入的文本框和2个用于用户选择绑定到数据库中的值的组合框。我的一个文本框设置为只读,因此用户只能在datagrid之外使用普通文本框和按钮填充它 当用户用数据填充DataGridView时,底部总是有一个空行;所以我禁用了它,并使用此代码阻止用户在datagrid中添加新行 dataGridView1.AllowUserToAddRows

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

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

当用户用数据填充DataGridView时,底部总是有一个空行;所以我禁用了它,并使用此代码阻止用户在datagrid中添加新行

dataGridView1.AllowUserToAddRows = false
我只想在用户单击上面提到的按钮时添加一个新行,该按钮会抛出一个错误

我收到的错误消息是:

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


带红色箭头的是组合框,带绿色箭头的是只读文本框

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

首先,在DataGridView中添加行:

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

int RowIndex = dataGridView1.RowCount - 1;
最后,设置其中的控件值:

DataGridViewRow R = dataGridView1.Rows[RowIndex];
R.Cells["YourName"].Value = tbName.Text;
如果datagrid的源是datattable,则必须在该表中添加行。为数据表中新添加的行指定新值,最后使用更新的datatable重新绑定datagrid

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

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

检查是否已正确设置新行的索引。可能这就是为什么会出现此错误。

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

绑定的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

我找到的最佳解决方案是:

//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();
来源:

您可以获取DataGridView的数据源并将其转换为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();

当我把这行写在表格上时

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

在这段代码之后,您可以轻松地添加行

hi Syeda,刚才我尝试了您的方法。在调试模式下,当进入第一步时出错,即dataGridView1.Rows.Add;我给出了gridview和数据表的示例。您在使用datatable时尝试了为gridview提供的示例。是否使用.DataSource属性?请注意,您还可以在顶部添加:dt.Rows.InsertAtrow,0;如果您在另一个方法中将DataTable绑定到DataGridView,并且DataTable不是公共变量,那么这是最好的解决方案。好主意!哦,天哪,它解决了我的问题。我被搜查了很长时间