Adding new rows to Datagrid
Hi all,
I am using an ASP.net datagrid to perform various modifications and update to an sql table.
This grid was created the same way it was specified in the
MSDN`s: Walkthrough: Using a DataGrid Web Control to Read and Write Data article.
The grid does not support adding new rows by default, so the article improvised a way by adding a row to the datasource(in my case the dataset) and rebind the data grid then set the newly added row to edit mode. So if my dataset initially contained two records, now it contains three! So far so good
But at some point the users decides to cancel the new row without typing anything inside the fields. T the cancel operation does not know that the new row is , well new, so it simply set the edit Index to -1 which forces the field to turn to labels even when it is still empty.
Now this is where it gets a little weird. Suppose that the user clicks on the add button again, the same operation of adding to datasource rebinding etc... Take effect again so now my Datasource contains four records instead of there. The moment the user hits the update the two new records are added to the database, remember there is a record that was canceled.
In order to avoid this problem I decided to refill my dataset using
sqlDataAdapter.fill(dataset). Which it is supposed to refresh the datasource from the database. In other words I am re-querying the database so I can get a clean dataset. Only this part is not working. No matter what I do I always, end up with the Dataset with the four records. Any Ideas?
|