Auto add row to child bo when add row to parent bo


Author
Message
Attila Dobozi
Attila Dobozi
StrataFrame Beginner (4 reputation)StrataFrame Beginner (4 reputation)StrataFrame Beginner (4 reputation)StrataFrame Beginner (4 reputation)StrataFrame Beginner (4 reputation)StrataFrame Beginner (4 reputation)StrataFrame Beginner (4 reputation)StrataFrame Beginner (4 reputation)StrataFrame Beginner (4 reputation)
Group: Forum Members
Posts: 4, Visits: 14
I have three business objects on a maintenance form, Parent, Child, Grandchild.  The Child and Grandchild are both filled for the Parent.  The Grandchild contains all rows that belong to all Child rows for the current Parent.  The Child.ChildAutoFilterOption = MatchCurrentRow.

The Parent is tied to the MaintenanceToolstrip.  The Child is tied to a second, custom, toolstrip.
When Parent changes the Child and Grandchild are filled appropriately.  Navigation and editing work.

When adding a Child I want to also add a Grandchild.  In the custom toolstrip cmdAdd.Click() function I call Child.AddNew().  The Child.AfterAddNew() event calls GrandChild.Add().  When I trace into the GrandChild.SetDefaultValues() I see that the field values show the previous GrandChild row data instead of the new row data (the GrandChild pk is not -1).  It looks like I'm overwriting the previous GrandChild row's data with default values. 

There is a GrandChild_Navigated event on the form where I messagebox the Child and GrindChild pks.  The messagebox does not popup after the GrandChild.AddNew() or GrandChild.SetDefaultValues().  The messagebox pops once with Child.pk = -1 and GrandChild.pk = 88 (prev GrandChild.pk value) and immediately pops again with Child.pk=-1 and GrandChild.pk = -1. 

... a day later ...

Never mind.  If I call GrandChild.Add() in the cmdAdd.Click() after calling Child.Add() it works.  It looks like the Child_AfterAddNew() event occurs before the Child filter and GrandChild navigation occurs so GrandChild defaults are assigned to the wrong row.  So the lesson is don't call AddNew() on a child bo from the parent's AfterAddNew event.  I'm evaluating SF so couldn't trace into its source otherwise I would've figured this out sooner.

Reply
Trent Taylor
Trent Taylor
StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 7K
I don't think the CopyDataFrom will work because that either adds new rows, which I don't want, or replaces the entire DataTable, again, not what I'm looking for.  I just want to update a couple of fields in the Parent current row. 

No.  You can definitely make the CopyDataFrom method work in this scenario.  Delete the record out the the current BO without updating the server:

'-- Remove the existing record
MyBo.DeleteCurentRow(True)
MyBo.CurrentDataTable.AcceptChanges()

'-- Get the new record
Using bo As New MyBoType
    bo.FillByPrimaryKey(changedPk)

   '-- Copy the data back into the source BO
   MyBo.CopyDataFrom(bo,AppendFromCurrentDataTable)
End Using

I solved this with a SqlCommand/SqlDataReader to fetch the fields for the current row then copying them from the reader back to Parent.currentRow.  Changing Parent field values set the form to edit mode so when I close the window it asks to save/cancel.  Is there a way to suppress changing edit states?

You can do that as well.  You cannot supress the states from changing but you can call AcceptChanges to accept the state of the data as it is in the BO which will then make the BO think that data in the BO and on the server are the same.

GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...





Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search