Need help with master/detail form
Hi, I'm trying to make WPF form which will display contacts and there details such as addresses. Form is bind to sql database with two tables tblContacts and tblContactsAddress.
Code:
Private cs As New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=PROFIL Service Database;Integrated Security=True")
Private da As New SqlDataAdapter("Select * FROM tblContactsProfil ORDER BY LastName", cs)
Private daAddress As New SqlDataAdapter("Select * FROM tblContactsProfilAddress", cs)
Private ds As New DataSet
Private MasterViewSource As CollectionViewSource
Private DetailViewSource As CollectionViewSource
Private WithEvents MasterView As BindingListCollectionView
Private DetailView As BindingListCollectionView
Private Sub ContactsWorkspace_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
da.Fill(ds, "tblContactsProfil")
daAddress.Fill(ds, "tblContactsProfilAddress")
Dim relation As New DataRelation("ContactsDetail", _
ds.Tables("tblContactsProfil").Columns("ContactID"), _
ds.Tables("tblContactsProfilAddress").Columns("ContactID"))
ds.Relations.Add(relation)
Me.MasterViewSource = CType(Me.FindResource("MasterView"), CollectionViewSource)
Me.DetailViewSource = CType(Me.FindResource("DetailView"), CollectionViewSource)
MasterViewSource.Source = ds
Me.MasterView = CType(Me.MasterViewSource.View, BindingListCollectionView)
Me.DetailView = CType(Me.DetailViewSource.View, BindingListCollectionView)
Me.InitialiseDataAdapter()
RecordsDisplay()
End Sub
Private Sub MasterView_CurrentChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MasterView.CurrentChanged
Me.DetailView = CType(Me.DetailViewSource.View, BindingListCollectionView)
End Sub
and xml:
Code:
<UserControl.Resources>
<CollectionViewSource x:Key="MasterView" />
<CollectionViewSource Source="{Binding Source={StaticResource MasterView}, Path='tblContactsProfilAddresses'}" x:Key="DetailView" />
</UserControl.Resources>
I set DataContext Binding Source of the main grid to StaticResource MasterView and ItemSource of the listbox to the StaticResource DetailView.
I found code above as solution for LinqToSql method. Only that I have change MasterViewSource.Source from query to my dataset. I don't know if I missing something or is this is the right approach but when i start my project there is no errors but also there is no data.
This is my first time I'm doing something with VS and with WPF and after having spent two days I'm going slowly crazy finding solution so please heeelp.