none
Many to Many (Students , classes example) DataGridComboBoxColumn binding problem RRS feed

All replies

  • Hello hanisafa,

    >> the DataGridComboBoxColumn don't display the name of the class the student registered in?

    For this, I guess if it is that you want when the first row in student DataGrid shows ‘Jim’ and the the first row in Class DataGrid should have the class values that Jim that has selected.

    If it is, my suggestion is that I will think of putting the two DataGrids to be one.

    >> When I register a student with new class (creating new row in the DB and selecting a class name ) when I try to save the changes to DB I got error message :Validation failed for one or more entities. See 'EntityValidationErrors' property for more details

    Could please share the detail exception message?

    Because the message ‘EntityValidationErrors’ would be caused by various reasons.

    It maybe that it already exists one entity that you want to add or others.

    Thanks &Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, September 25, 2013 3:44 AM
    Moderator
  • Thanks for your replay ,

    I've uploaded the project , its very simple and straightforward,

    link to project code

    what i want is to be able to select a student and add/remove classes (register and unregistered the student from classes )

    or the alternative , select a class and add/remove student from it

    using 2 DataGirds (maser / details)

    Notes about the code:

    1: don't forget to update the path to dataBase file in the connection string in App.config

    2: if you regenerate the EF class , don't forget tot replace HashSet,ICollection with ObservalbeCollection in Class.cs and Student.cs



    Wednesday, September 25, 2013 7:45 AM
  • Hi,

    I download it and test it with some data like below:

    Stuent:

    1	BMX	111111

    Class:

    1	Math	03:00:00
    2	Sports	04:00:00
    3	English	00:00:00

    StudentClass:

    1	1

    And I do some changes to the SearchStudentClasses_Click method because when I click the button it throws an exception, so I changed the query to a List result like below:

    var q = (from student in dc.StudentSet
                         //where student.Name.Contains("")
                         select new
                         {
                             q2 = from cla in student.Classes
                                  where cla.Name.Contains("m")
                                  select new { cla.Id, cla.Name, cla.Duration }
                         }).ToList();
    
                StudentclassesDataGrid.ItemsSource = q;
    And then it can work well.

    The result is:

    If it is what you want.

    And for the message "EntityValidationErrors", I find that if I add an new item not completely data,  like below:

    The error will occur, so make sure that you add a complete item.

    Thanks & Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Wednesday, September 25, 2013 10:04 AM
    Moderator
  • Thanks for you time ,

    for the error you are right , but there is 3 problems:

    A: when you select BMX , the detials grid dont' show the name of the class

    B: for exsamble , if BMX have math class , select BMX and change the class to Sports , hit save -> ok message , but the changes never saved , course , close the application and restart it (or in the grid below ) BMX will have math class not Sports 

    c: try to add another class to BMX , the error message will appear again

    Wednesday, September 25, 2013 10:35 AM
  • Hi,

    From your description, it seems that what your want is more regarding implementing a bidirectional selected function using WPF, I will involve someone experienced WPF to help us.

    If we have the result, I will feed it back as  soon as possible.

    And I recommend that you psot this issus to the WPF forum.

    Thanks for your understanding.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.



    Thursday, September 26, 2013 8:10 AM
    Moderator
  • appreciate your time and effort,

    I've already tried with WPF forum but with no luck ,i thought this is very basic.

    mean time i found a work around , it works but not sure its the right one

    I've changed the cell type to  DataGridTemplateColumn

    <DataGridTemplateColumn Header="Student Name"
                                                    Width="*">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Name,UpdateSourceTrigger=PropertyChanged}" />
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
    
                                <DataGridTemplateColumn.CellEditingTemplate>
                                    <DataTemplate>
                                        <StackPanel>
                                            <ComboBox ItemsSource="{Binding Source={StaticResource studentsNamesForClaasesViewSource}}"
                                                      DisplayMemberPath="Name"
                                                      DropDownClosed="ComboBox_DropDownClosed" />
                                        </StackPanel>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellEditingTemplate>
                            </DataGridTemplateColumn>

    and had to add event for ComboBox_DropDownClosed
    private void ComboBox_DropDownClosed(object sender, EventArgs e)
        {
          var addedStudent = ((CollectionViewSource)(this.FindResource("classStudentsViewSource"))).View.CurrentItem as Student;
          var selctedStudent = (sender as ComboBox).SelectedItem as Student;
    
          if (addedStudent != null)
          {
            (classDataGrid.SelectedItem as Class).Students.Remove(addedStudent);
          }
          if (selctedStudent != null)
          {
            (classDataGrid.SelectedItem as Class).Students.Add(selctedStudent);
          }
        }

    if you got any result , please let me know


    Thursday, September 26, 2013 9:44 AM
  • Hi Hani Safa,

    This is a Data Plat Forum.

    And it seems that what you want is completely WPF function implementation.

    So I recommend you posting your question with information that you have posted here to the WPF forum since there are professional person to deal them.

    And I do not think it is basic because it has bilateral logic.

    Regards.

    Friday, September 27, 2013 8:25 AM