none
data binding - many to many relationship with datagrid , adding and removing relation RRS feed

  • Question

  • I have a WPF / entity framework application, my model have student , class entities there is a many to many association/relation between student and class (student can have many classes and class have many students )

    student [ Id ,Name , Classes(Navigation property) ] classes [ Id ,Title, Students(Navigation property ]

    in this case the entity framework don't show the join/relation table.

    i have a 2 DataGirds as master details, student grid is the master and classes is details

    how can i set the binding to classes gird (the details gird) so i can add and remove classes to the selected student?

    what i want is to select a student from the master grid and add or remove his classes

    this is my classes gird

    <DataGrid x:Name="classesDataGrid"
                          AutoGenerateColumns="False"
                          EnableRowVirtualization="True"
                          Height="200"
                          ItemsSource="{Binding Source={StaticResource studentClassesViewSource}}"
                          RowDetailsVisibilityMode="VisibleWhenSelected"
                          Width="380">
                    <DataGrid.Columns>
    
    
                        <DataGridComboBoxColumn Header="Class Name From Combo"
                                                Width="*"
    
                                                ItemsSource="{Binding Source={StaticResource classViewSource}}" 
                                                DisplayMemberPath="Name"
    
                                                SelectedItemBinding="{Binding Students}" 
    
                                                />
                        <!--<DataGridTextColumn x:Name="durationColumn"
                                            Binding="{Binding Duration}"
                                            Header="Duration"
                                            Width="SizeToHeader" />-->
    
                    </DataGrid.Columns>

    This scenario works fine when the relation is one to many but i couldn't make it work for many to many relations .

    Friday, June 28, 2013 11:50 AM

All replies

  • A ComboBox can only have one selected value but in this case you could use a DataGridTemplateColumn with an ItemsCollection to display the list of classes for the selected student. To remove a class item from a student object you can then add a button for each class that removes the corresponding item from the collection.
    Friday, June 28, 2013 12:30 PM
  • thanks for you answer ,actually my problem is how do i add/edit classes to student in Datagrid view

    i can list all the classes in the combobox and the combobox display the correct class for the current student

    but i can't edit or add new classes .

    of course this also apply to adding students to a class

    Friday, June 28, 2013 12:51 PM