Nandhi
Nandhi

Reputation: 495

Bind two related DataSets to two DataGrids

I have a problem binding a DataSet with two tables related, I have been searching for many time. I am not sure how to do this with a WPF DataGrid (it seems to be different than with a WinForms DataGrid).

I am using MVVM pattern and have my DataContext associated with my DataSet, one grid is bound on ItemsSourceProperty to one of the tables. I am wondering how you select one item in dataGrid1 and automatically bind the related rows to dataGrid2.

Upvotes: 0

Views: 630

Answers (1)

blindmeis
blindmeis

Reputation: 22435

all you need is to know your Relation Name. let us assume the tables are Student and Classes and the relation name is: FK_Student_Classes, then your binding looks like this:

    <DataGrid x:Name="grdStudents" ItemsSource="{Binding MyDataSet.Student}" AutoGenerateColumns="True" Grid.Row="0"/>
    <DataGrid ItemsSource="{Binding ElementName=grdStudents, Path=SelectedItem.FK_Student_Classes}" Grid.Row="1"/>

when ever you select a row in the student grid you will see all related rows for in your classes grid.

Upvotes: 2

Related Questions