首先,我在here查找了这个相关问题,但解决方案
dataGridView1.Rows.Add()
在我的情况下不起作用。
在我的 Datagridview 中,我有 3 个用于数据输入的文本框和 2 个用于用户选择值的组合框(已绑定到数据库中)。我的一个文本框设置为只读,以便用户只能在数据网格之外填充它(使用普通的文本框和按钮)。
当用户向DataGridView填充数据时,底部总是有一个空行;所以我禁用了此功能,并使用此代码来防止用户在数据网格内添加新行...
dataGridView1.AllowUserToAddRows = false
我只想在用户单击上面提到的按钮(这会引发错误)时添加新行。
我收到的错误消息是:
“当控件进行数据绑定时,无法以编程方式将行添加到 datagridview 的行集合中”
带红色箭头的为 ComboBox,带绿色箭头的为只读 TextBox
看起来好像您正在使用
DataSource
的 DataGridView
属性。当此属性用于绑定数据时,您无法直接向 DataGridView
显式添加行。您必须将行直接添加到您的数据源。
例如,如果您的数据源是
DataTable
,则使用分配给 DataTable
属性的 DataSource
(未经测试):
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
的 DataSource
并将其投射为 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();
添加新行后,您必须在行计数边界中设置行索引。 你必须执行这些步骤。
首先,在DataGridView中添加行:
dataGridView1.Rows.Add();
其次,将新行索引设置为计数 - 1:
int RowIndex = dataGridView1.RowCount - 1;
最后,设置其中的控件值:
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();
检查新行的索引是否设置正确。也许这就是您收到此错误的原因。
绑定 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();
来源:http://www.codeproject.com/Questions/615379/Adding-rows-to-datagridview-with-existing-columns
当我在 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();
}
}
}
在此代码之后,您可以轻松添加行