Problem binding DataRelation to ListView ItemsSource


  • 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}"  
              <GridViewColumn Header="Street"
    ="{Binding Path=Street}"/>  

    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  
    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)  
    End Sub     

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSave.Click  
    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.


    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.


    • Changed type Tao Liang Monday, December 22, 2008 2:19 AM
    • Changed type Tao Liang Monday, December 22, 2008 2:20 AM
    • Changed type Tao Liang Friday, January 16, 2009 1:58 AM
    Monday, December 15, 2008 3:46 PM

All replies

  • Hi:

    Can you send a full version of code?
    Then I can compile and debug it.
    Monday, December 22, 2008 2:16 AM
  • You can send email to
    Monday, December 22, 2008 2:17 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.

    Monday, December 22, 2008 2:20 AM
  • Hi, I have sent you my code via email. Did you receive it?
    Wednesday, January 14, 2009 5:29 PM
  • I am sorry I do not receive your file.
    Can you send to me again? Try the email below:

    Friday, January 16, 2009 1:59 AM
  • Hi, I have just forwarded you the emails which I sent you the last time. Pls check. Thanks! :)
    Friday, January 16, 2009 3:50 PM
  • Hi, 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?
    Tuesday, March 15, 2011 6:08 AM