none
BindingSource.EndEdit() on new parent row removes new child row that is associated with it. RRS feed

  • Question

  • I have a TableAdapter with two tables, a Parent and a Child table.  The child table contains a column called ParentId that is a FK to the Parent table.  The table adapter contains a relation between the two tables for the ParentId column.  The relation is configured as 'Both Relation and Foreign Key Contraint' with the Update Rule = Cascade.  I am attempting to create a new row in the parent table and at the same time a new row in the child table.  The application is a WinForm UI with a grid for the parent and a grid for the child.  I have two BindingSources one for the Parent and one for the FK relationship for the Child table.  In the grids I see the rows getting created correctly and the ParentId's match.

    The issue that I am having is that when I attempt to save the data I first issue a EndEdit on the parent BindingSource and then an EndEdit on the child BindingSource.  When the EndEdit on the parent BindingSource is issued the new row associated with the Child table disappears.  Why does EndEdit on the parent BindingSource delete the associated child row?

    Thanks in advance,
    Terry


    twahl
    Tuesday, July 13, 2010 5:34 PM

Answers

  • My experience with parent and child rows is that you need to EndEdit a new parent row before attempting any modifications to an associated child row. This is because the parent row must actually be present in the DataTable in order to relate a child row to it.  In other words, the parent row must be in the DataTable with DataRowState.Added.  Prior to the EndEdit it has DataRowState.Detached and is not in the table at all.

    I agree that this aspect of data binding can be very frustrating!  It makes it difficult to build UI's in the exact way that you would prefer.  One somewhat radical thing you can do is turn EnforceConstraints to false in the DataSet and then call EndEdit immediately after you create the new row.  You can turn on EnforceConstraints later (and handle exceptions), perhaps shortly before you save the data.

     

    Wednesday, July 14, 2010 12:26 AM

All replies

  • My experience with parent and child rows is that you need to EndEdit a new parent row before attempting any modifications to an associated child row. This is because the parent row must actually be present in the DataTable in order to relate a child row to it.  In other words, the parent row must be in the DataTable with DataRowState.Added.  Prior to the EndEdit it has DataRowState.Detached and is not in the table at all.

    I agree that this aspect of data binding can be very frustrating!  It makes it difficult to build UI's in the exact way that you would prefer.  One somewhat radical thing you can do is turn EnforceConstraints to false in the DataSet and then call EndEdit immediately after you create the new row.  You can turn on EnforceConstraints later (and handle exceptions), perhaps shortly before you save the data.

     

    Wednesday, July 14, 2010 12:26 AM
  • Hi twahl,

     

    I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions? 

     

    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, July 20, 2010 1:05 AM
    Moderator
  • Hello all,

    I know this is an old thread, but have you tried calling the parentbindingsource.endedit on the childbindingsource.addingnew event?

    Dwight


    Dwight Miller

    Friday, July 13, 2012 4:08 PM
  • Hi,

    You should use BindigSource.Addnew() inseat of DataView.Addnew(), and then access the new DatarowView via BindingSource.Current.
    You can set the initial values for the DataRowView in the BindingSource.AddingNew event, then you can set the other values after the call of the BindingSource.Addnew().

    hope it helps.

    rgrds,

    Balazs

    Thursday, October 3, 2013 8:10 AM
  • This is a very old thread, Balazs. And the OP said nothing about using DataView.AddNew(). In fact he never mentioned anything about the DataTable/DataView at all, just parent and child BindingSources. 

    However, even though this is an old thread, I thought it might be a good idea to post a link to my blog post about uncommited child DataTable changes. Not only does my explain how to get around the problem, but I also explain why it happens. Take a look:

    http://geek-goddess-bonnie.blogspot.com/2010/03/uncommitted-child-table-changes.html


    ~~Bonnie Berent DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Thursday, October 3, 2013 3:09 PM