簡體   English   中英

當控件為數據綁定時,無法以編程方式將行添加到 datagridview 的行集合中

[英]Rows cannot be programmatically added to the datagridview's row collection when the control is data-bound

首先,我在這里查找了這個相關的問題,但解決方案dataGridView1.Rows.Add()在我的情況下不起作用。

在我的 Datagridview 中,我有 3 個用於數據輸入的 TextBoxes 和 2 個 ComboBoxes 供用戶選擇值(綁定到數據庫中)。 我的一個 TextBoxes 設置為只讀,以便用戶只能在數據網格之外填充它(使用普通的 TexBox 和一個按鈕)。

當用戶用數據填充DataGridView時,底部總是有一個空行; 所以我禁用了它並使用此代碼來防止用戶在數據網格中添加新行...

dataGridView1.AllowUserToAddRows = false

我只想在用戶單擊我上面提到的按鈕時添加一個新行(這會引發錯誤)。

我得到的錯誤信息是:

“當控件為數據綁定時,無法以編程方式將行添加到 datagridview 的行集合中”

示例圖像 帶紅色箭頭的是 ComboBox,帶綠色箭頭的是只讀 TextBox

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

您可以獲取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();

添加新行后,您必須在行數邊界設置行索引。 您必須執行這些步驟。

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

檢查您是否正確設置了新行的索引。 也許這就是您收到此錯誤的原因。

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

我發現的最佳解決方案:

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

在此代碼之后,您可以輕松添加行

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM