Problem binding DataRelation to ListView ItemsSource
-
Monday, December 15, 2008 3:46 PM
I have a ListView whose ItemsSource binds to a child table (Street). Area is the parent table and Street is the child table in ADO.NET strongly typed dataset. I have established relationship FK_Area_Street and set the DataRelation in the DataSet designer to "Both Relation and Foreign Key Constraint" and the Update and Delete rules to Cascade.<ListView Name="ListView" ItemsSource="{Binding Path=FK_Area_Street}" IsSynchronizedWithCurrentItem="True"> <ListView.View> <GridView> <GridViewColumn Header="Street"
DisplayMemberBinding="{Binding Path=Street}"/></GridView> </ListView.View> </ListView>
Meanwhile in my VB.NET code, the DataContext of my WPF form is bound to my parent table.Private WithEvents View As BindingListCollectionView Private DetailView As BindingListCollectionView DataContext = Me.DataSet.Area View = CollectionViewSource.GetDefaultView(Me.DataContext) DetailView = CollectionViewSource.GetDefaultView(Me.ListView.ItemsSource)
My problem is quite complicated. Pls allow me to explain the errors and issues below.
I need to add new parent record (Area), followed by new child record (Street). I call AddNew and CommitNew on my parent and child views respectively and finally call UpdateAll to save the newly added values back to the database.Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAdd.Click Me.View.AddNew() Me.View.CommitNew() End Sub Private Sub btnAddDetail_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAddDetail.Click Me.DetailView = CollectionViewSource.GetDefaultView(Me.ListView.ItemsSource) Me.DetailView.AddNew() Me.DetailView.CommitNew() End Sub Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSave.Click taManager.UpdateAll(Me.DataSet) End Sub
Scenario 1
Sequence of calling the methods:
a) View.AddNew, View.CommitNew, key in values
b) DetailView.AddNew, DetailView.CommitNew, key in values
c) UpdateAll
Problem: Newly added child values in ListView mysteriously "disappear" from my UI after UpdateAll. Newly added parent values are still there.
At the point of calling UpdateAll, the newly added values in my ListView "disappear" from my UI. These values, though they disappear from my UI, are being saved in the database if I call UpdateAll. How I know is because when I restart my application and navigate to the newly added parent record, both the parent and child record are displayed - which means add new methods on View and DetailView work.
When I navigate my View using Me.View.MoveCurrentToPrevious() and Me.View.MoveCurrentToNext(), I can see all other parent and child values except my newly added child views from calling DetailView.AddNew, DetailView.CommitNew then UpdateAll.
My little workaround:
After UpdateAll, refill the Table Adapters.DataSet.Clear() taArea.Fill(DataSet.Area) taStreet.Fill(DataSet.Street) Me.View.MoveCurrentToLast()
The problem is solved with refilling of table adapters but I find it inefficient to perform table adapters' Fill method after every UpdateAll.
Scenario 2
If I call UpdateAll after adding parent then proceed to add child, I get an error.
Sequence of calling the methods:
a) View.AddNew, View.CommitNew, key in values
b) UpdateAll
c) DetailView.AddNew, DetailView.CommitNew
d) UpdateAll
Error occurs at DetailView.CommitNew. ForeignKeyConstraint FK_Area_Street requires the child key values (-1) to exist in the parent table.
I have to manually set the Foreign Key Me.DetailView.CurrentItem("AreaID") = Me.MasterView.CurrentItem("AreaID") before the line DetailView.CommitNew to solve the FK constraint error. The FK is not updated in ListView though I have set Update rule to cascade.
Scenario 3
Sequence of calling the methods:
a) DetailView.AddNew, DetailView.CommitNew, key in values
b) UpdateAll
No problem.
Scenario 4
Sequence of calling the methods:
a) View.AddNew, View.CommitNew, key in values
b) UpdateAll
No problem.
***
I suspect DetailView or ListView ItemsSource is unable to establish relationship (FK_Area_Street) with newly added rows in View. Hence DetailView has a NullReference when View CurrentItem is a newly added row.
What can I do? Pls help as I am stuck with this problem for more than a week and I consulted WPF guru Beth Massi but I still can't solve my problem.
Footnote: Everything is working fine with navigating the records, delete and update operation. I only have problems with adding new parent and child records.
Thanks.
All Replies
-
Monday, December 22, 2008 2:16 AMHi:
Can you send a full version of code?
Then I can compile and debug it.
Regards!
-
Monday, December 22, 2008 2:17 AMYou can send email to liangtom@gmail.com
-
Monday, December 22, 2008 2:20 AM
We are changing the issue type to “Comment” because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to “Question” by clicking the "Options" link at the top of your post, and selecting "Change Type" menu item from the pop menu. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.
-
Wednesday, January 14, 2009 5:29 PMHi, I have sent you my code via email. Did you receive it?
-
Friday, January 16, 2009 1:59 AMI am sorry I do not receive your file.
Can you send to me again? Try the email below:
liangtom@gmail.com
liangtao21990@sohu.com
-
Friday, January 16, 2009 3:50 PMHi, I have just forwarded you the emails which I sent you the last time. Pls check. Thanks! :)
-
Tuesday, March 15, 2011 6:08 AMHi, I know it's more than two years later, but I could not find an answer to this anywhere. I have the exact same problem! Did you ever find a way around this?

