Results 1 to 5 of 5

Thread: [RESOLVED] Bound datagridview not bound?

  1. #1

    Thread Starter
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Resolved [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?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Bound datagridview not bound?

    As I always say, read the documentation:
    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    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
  •  



Click Here to Expand Forum to Full Width