none
Many to Many (Students , classes example) Datagrid xaml binding 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 .

    I've found a similar post on this problem on msdn, but the conclusion that this scenario can't be done (in datagrid at least).


    Monday, August 26, 2013 5:24 PM

All replies

  • Hello,

    For your question, i think maybe we can implement this from code-behind

    #Master-Detail Data Binding in WPF with Entity Framework
    http://blogs.msdn.com/b/bethmassi/archive/2009/07/14/master-detail-data-binding-in-wpf-with-entity-framework.aspx

    #Insert/Update Many to Many Entity Framework . How do I do it?
    http://stackoverflow.com/questions/4253165/insert-update-many-to-many-entity-framework-how-do-i-do-it

    Hope it can help.



    Best Regards,
    A Beginner :)
    Please remember to mark the replies as answers if they help

    Tuesday, August 27, 2013 9:43 AM
  • Obviously you can't use a single ComboBox with classes per student if there is a many-to-many relationsship between the two entities.

    It would only make sense to use a ComboBox in the DataGrid that displays a row per student if it supported selecting multiple items. Then you would be able to select multiple classes per student. WPF doesn't ship with any such control, but you could create your own one: http://www.codeproject.com/Articles/45782/A-WPF-Combo-Box-with-Multiple-Selection

    Tuesday, August 27, 2013 10:15 AM
  • thanks for your replay ,

    the gird that I'm showing its XAML is to display the the classes each student has registered in

    the image will display better



    when i select a student from the left grid , all the classes he rigesterd in showen in the right gird

    the right gird (the one i provide the XAML) has

    ItemsSource="{Binding Source={StaticResource studentClassesViewSource}}"

    the studentClassesViewSource is bound as a details as her:


    <CollectionViewSource x:Key="studentViewSource"
                                  d:DesignSource="{d:DesignInstance {x:Type local:Student}, CreateList=True}" />
            <CollectionViewSource x:Key="studentClassesViewSource"
                                  Source="{Binding Classes, Source={StaticResource studentViewSource}}" />

    the problem is: when i create new row and select a class for the student and call SaveChanegs()

    i got this error

    Validation failed for one or more entities. See 'EntityValidationErrors' property for more details


    • Edited by Hani Safa Wednesday, August 28, 2013 10:27 PM
    Wednesday, August 28, 2013 10:06 PM
  • thanks for your replay

    my problem is with many to many grid data binding ,

    i've added more info to

    Avatar of Magnus (MM8)

    Magnus (MM8)

    replay

    Wednesday, August 28, 2013 10:07 PM