locked
WPF and Entity Framework without MVVM RRS feed

  • Question

  • Hi to all the ML.

    I'm trying to make my first application on db. I chose WPF and Entity Framework (database first). I can connect to the db and view the data coming from the individual tables on the differents datagrid. But now I'm stuck. I would like to visualize the data of two tables linked by a one to many relationship on a single datagrid. It's possible?

    Friday, September 28, 2018 8:52 AM

Answers

  • Hi Jotric1978,

    You could create join query, which join two table into a list, then you could bind the list to the WPF datagrid control.

    #Model Class.

    public class Student { public int Id { get; set; } public string Name { get; set; }

    public int GradeID { get; set; } public virtual Grade Grade { get; set; } } public class Grade { public int GradeID { get; set; } public string GradeName { get; set; } public string Section { get; set; } public virtual ICollection<Student> Student { get; set; } }

    #Query.

    var result = from p in db.Grade join d in db.Student on p.GradeID  equals d.GradeID  select new { GradeName = p. GradeName,

    //other column name };


    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Jotric1978 Monday, October 1, 2018 7:40 AM
    Monday, October 1, 2018 2:20 AM

All replies

  • IMO, it's more of a question for the WPF forum.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=wpf

    Friday, September 28, 2018 12:13 PM
  • Hi Jotric1978,

    You could create join query, which join two table into a list, then you could bind the list to the WPF datagrid control.

    #Model Class.

    public class Student { public int Id { get; set; } public string Name { get; set; }

    public int GradeID { get; set; } public virtual Grade Grade { get; set; } } public class Grade { public int GradeID { get; set; } public string GradeName { get; set; } public string Section { get; set; } public virtual ICollection<Student> Student { get; set; } }

    #Query.

    var result = from p in db.Grade join d in db.Student on p.GradeID  equals d.GradeID  select new { GradeName = p. GradeName,

    //other column name };


    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Jotric1978 Monday, October 1, 2018 7:40 AM
    Monday, October 1, 2018 2:20 AM
  • I've made some adjusts for my case and now I've the right query within data. But now how can I made a bindign to my datagrid? La mia query è questa:

            Dim db As New SRMdbEntities()
            Dim result = From p In db.Tbl_Plant
                         Join d In db.Tbl_Product On p.EntryMaterialType Equals d.IDProduct
                         Select New With {p.IDPlant,
                                          p.DescriptionPlant,
                                          d.DescriptionProduct,
                                          p.EntryMaterialType}
    
    
      Dim query = result.ToList()

    I've tried with:  dgvPlant.DataContext = query

    but no luck


    • Edited by Jotric1978 Monday, October 1, 2018 1:05 PM
    Monday, October 1, 2018 9:31 AM
  • Hi Jotric1978,

    I am not sure how you binding the elements, please debug your application and check if it has records in variable named query. if it has records, please try the following code.

    dgvPlant.ItemsSource = query

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 2, 2018 2:17 AM
  • Hi Zhanglong,

    as I told the query works fine and inside I can see my elements. I've already tried with the code you suggested me, but no luck too.My XAML code is very simple:

     <DataGrid Grid.Row="0" Grid.Column="1" x:Name="dgvPlant" AutoGenerateColumns="True"  Margin="10,0,0,20" ItemsSource="{Binding}" >

    Tuesday, October 2, 2018 6:19 AM
  • Hi Jotric,

    Could you please share a simple sample via OneDrive.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 3, 2018 8:04 AM
  • Ok uploaded a simple solution with a simple db in Google Drive.

    https://drive.google.com/open?id=13Ait28tze9Fs7gtcZJmHntPq22J9DIFK

    Now I can making a binding, but the XAML have some problems... it dosen't work fine.

    In the solution you can see how works the first datagrid (based on EntityFramework) and the second, with the itemssource in bnding with the linq query.

    Thursday, October 4, 2018 1:44 PM
  • Hi,

    Based on your sample, I modify the following two places, which works as expected.

    1. Xaml (Remove pr in ItemSource binding)

    <DataGrid Grid.Row="0" Grid.Column="1" x:Name="DgvPlant" AutoGenerateColumns="False" ItemsSource="{Binding}"  Margin="0,0,0,10" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserResizeRows="False" CanUserSortColumns="False" IsSynchronizedWithCurrentItem="True" IsHitTestVisible="False" IsManipulationEnabled="True">

    2. Code Behind.

    Private Sub PopulateDataGridViewPlant()
    
    
            Dim result = From p In dBEntities.Tbl_Plant
                         Join d In dBEntities.Tbl_Product On p.EntryMaterialType Equals d.IDProduct
                         Select New With {p.IDPlant,
                                          p.DescriptionPlant,
                                          d.DescriptionProd,
                                          p.EntryMaterialType}
    
    
            Dim query = result.ToList()
            DgvPlant.DataContext = result.ToList()
    
        End Sub

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 5, 2018 7:12 AM
  • Thanks for the changes, but it seems (to me) that the DgvPlant does not work well. I can not select any items.

    Where am I doing wrong?

    Friday, October 5, 2018 10:12 AM
  • Hi Jotric1978,

    >>Thanks for the changes, but it seems (to me) that the DgvPlant does not work well. I can not select any items.Where am I doing wrong?

    According to your description, it seems that it is a new issue, which related to WPF, I would suggest that you could post a new thread in WPF forum for suitable support.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=wpf

    Thanks for your understanding.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, October 8, 2018 1:40 AM