views:

690

answers:

3

I have a datagridview which is initially bound to an empty datatable. Then the first row is populated by setting values to the columns of the grid's CurrentRow.DataBoundItem row. I enter a new row manually by typing values in the last blank row. I hit a save button which does: DataTable dt = (DataTable)MyGrid.DataSource.......

I noticed the number of rows in dt is only 1 instead of the two showing in the grid. However if I hit the Enter key first on the row and then click save, the number of rows is 3. The two filled rows plus the new row caused by the Enter key.

How do I get the two rows placed in the datatable without hitting the Enter key? (I don't want to programmatically send an Enter keystroke)

I tried to call the EndEdit() and UpdateCellValue() to accept the new row but the datasource still shows only one row.

A: 

I answered a similar question recently, you should use Form.Validate() to push data back to the underlying business object (your DataTable) before saving your changes to the database.

Julien Poulin
I was already using Validate()
Tony_Henrich
A: 

As Jason mentioned make sure you're rebinding the grid after you insert into your DataTable.

myGrid.DataSource = dt;
myGrid.DataBind(); <-----
Jreeter
I am not binding to a datatable. I am getting a reference from the grid's data source to a datatable to get the values from the grid.
Tony_Henrich
A: 

I had to add a binding source which the grid binds to instead of binding to a datatable.

Tony_Henrich