|
-
Jul 9th, 2010, 02:26 AM
#1
[RESOLVED] Bound datagridview not bound?
There is something strange going on with my datagridview.
I've a data adapter that fills the underlying data table from a database.
Then I assign this datatable as datasource to the datagridview
Then I add a row to the datagridview and immediately I need to update the table in my database (as soon as user leaves the newly created row).
At this moment I have more rows (+2, not +1) in my datagridview than in the underlying datatable (the new row doesn't exists yet in it so the data adapter .Update method doesn't write anything to the database).
When DataGridView's .RowLeave event fires the undelying datatable is not yet updated. I've a question - is this behavior normal?
Last edited by cicatrix; Jul 9th, 2010 at 02:36 AM.
-
Jul 9th, 2010, 02:39 AM
#2
Re: Bound datagridview not bound?
I would assume that those two rows are the data entry row at the bottom of the grid and the new row that has been created but not yet added to the DataTable. What event are you using to initiate the save? There is no event that is raised specifically when a new row is added to a bound DataTable. I'd think that the ListChanged event of the BindingSource would be the best option. You'd check whether there's a new row in the DataTable and save if there is. You might also choose CurrentChanged if you want to save immediately after editing a row too.
-
Jul 9th, 2010, 02:51 AM
#3
Re: Bound datagridview not bound?
The datatable has an event .TableNewRow which I use to fill the pre-computed data in the datagridview cells, but I assumed that since the .TableNewRow had fired already then the new row exists in the datatable, but no. RowLeave event of the datagridview (which I used), apparently fires too early.
Well, the problem seems to be resolved when I added a boolean flag in a .TableNewRow and used .RowEnter event of the datagridview.
Apparently, the undelying datatable is updated in between the .RowLeave and .RowEnter events of the datagridview.
I thought that the changes take place immediately, but no.
-
Jul 9th, 2010, 02:56 AM
#4
Re: Bound datagridview not bound?
As I always say, read the documentation:
 Originally Posted by MSDN
Fires after a new DataRow has been created using NewRow. This event is fired before the invoked NewRow method returns. The new DataRow instance is detached; it has not been added to the collection.
TableNewRow is rasied when a new row is created, not when it's added to the table. It makes perfect sense when you think about it. A brand new row can't necessaruily be added to the table because there are all sorts of constraints it might have to satisfy: non-null values, foreign keys, etc. When a new row is added in a grid, it can't be safely added to the table until you navigate away from that row, which is prevented if any constraints are not satisfied.
-
Jul 9th, 2010, 03:10 AM
#5
Re: Bound datagridview not bound?
I gathered. Still it'd be much more easier if the DataTable class had .RowAdded event as well.
This raises one more issue. Is there any way I could track the addition of a new row to the data table BEFORE the RowEnter event of the datagridview?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|