2012-01-03 46 views
10

首先,我在here中查找了相關的問題,但解決方案dataGridView1.Rows.Add()在我的情況下不起作用。當控件是數據綁定時,無法以編程方式將行添加到datagridview的行集合

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

當用戶用數據填充DataGridView時,底部總是有一個空行;所以我禁用該功能,我用這個代碼,以防止用戶添加數據網格中新的行...

dataGridView1.AllowUserToAddRows = false 

我只是想添加一個新行,當用戶點擊我上面提到的按鈕(拋出一個錯誤)。

我得到的錯誤信息是:

「行不能以編程方式添加到DataGridView的行集合時,該控件是數據綁定」

sample image 了一個用紅色箭頭是一個組合框,並且帶有綠色箭頭的是隻讀文本框

+5

添加一行到數據源 – sq33G 2012-01-03 05:00:56

+0

是否使用了.DataSource財產? – 2012-01-03 16:09:47

回答

20

看起來好像您正在使用DataGridView的DataSource屬性。當使用此屬性綁定到數據時,您無法直接將行直接添加到DataGridView。您必須將行直接添加到您的數據源。

例如,如果您的數據源是一個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); 
} 
+2

請注意,您也可以在頂部添加:'dt.Rows.InsertAt(row,0);' – TaW 2016-10-02 15:31:58

1

添加新行後,必須將行索引設置爲行計數的邊界。 您必須執行這些步驟。

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

    dataGridView1.Rows.Add(); 
    
  2. 二,設置新行的索引數 - 1:

    int RowIndex = dataGridView1.RowCount - 1; 
    
  3. 然後在最後,設置控件值它:

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

如果您的datagrid的源是可數據表,則必須在該表中添加行。爲數據表中新添加的行賦予新值,最後使用更新的數據表重新綁定數據網格。

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

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

檢查是否已正確設置新行的索引。也許這就是你得到這個錯誤的原因。

+1

嗨Syeda,剛纔我嘗試了你的方法。在調試模式下,錯誤發生在進入第一步時,這是'dataGridView1.Rows.Add();' – 2012-01-03 06:12:43

+1

我給了gridview和數據表的樣本。您在使用數據表時嘗試使用gridview提供的示例。 – Syeda 2014-03-27 12:23:48

0

的綁定的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 
0

我發現最好的解決辦法記住永遠不要將數據添加到datagridview的編程:

//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

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

這是最好的解決方案,如果您在另一個方法中將'DataTable'綁定到'DataGridView', DataTable'不是一個公共變量。很好的思想! – 2016-11-08 05:25:42

相關問題