Programatically adding a row to a DataGridView that is bound to a datasource and already has columns
I've been trying the whole evening to programatically add rows to my DataGridView but I can't seem to get it working I tried lots of differents ways but none of them seem to be working.
I get no errors, everything looks fine.
I have a WinForm, I added a DataGridView, bound a DataSource to it (table from a SQL 2008 connection).
The data shows up in it, everything is fine but I want to process the data before showing it, so I want to programatically add ro开发者_运维技巧ws myself.
Here's what I tried so far:
string[] rowArray = new string[]{ "Test", "Test", "Test" };
dataGridView.Rows.Add(rowArray[0]);
-
dataGridView.Rows.Add();
int newRowIndex = dataGridView.RowCount - 1;
DataGridViewRow newRow = dataGridView.Rows[newRowIndex];
newRow.Cells["PurchaseOrderId"].Value = "Test";
newRow.Cells["SupplierId"].Value = "Test";
newRow.Cells["State"].Value = "Test";
-
DataRow newRow = purchaseOrders.NewRow(); // purchaseOrders = this.purchaseOrderManagerDataSet.PurchaseOrders
newRow["PurchaseOrderId"] = "Test";
newRow["SupplierId"] = "Test";
newRow["State"] = "Test";
dataGridView.Rows.Add(newRow);
None of them adds something to the DataGridView.
Any idea of what I'm doing wrong?
Thanks a lot!
You should modify the datasource object.
For example if you have a BindingList
, you can simply add a new object to the list, and it will show in the DataGridView
.
精彩评论