[UWP] [EF] How to populate DataGrid with data using EF

BitSmithy 1,956 Reputation points


I dont know How to populate DataGrid with data using EF. In my app I use classes:

DataGrid myDG;

public class myDbContext : DbContext 
    public string connectionString;

    public DbSet<MyClass> MyTable{ get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. Xiaodi Yan 876 Reputation points MVP

    First, you need to query the data from your dbContext:

    using (var db = new BloggingContext())  
        var blogs = db.Blogs  
            .Where(b => b.Rating > 3)  
            .OrderBy(b => b.Url)  

    Then, you need to add the DataGrid control to your XAML page and set the data-binding. I'm not sure what DataGrid control you are using. I suppose you can use the DataGrid from Windows Community Toolkit.

    Install this nuget package from the NuGet Package Manger:

    Add the control and set the data-binding:

    <controls:DataGrid x:Name="dataGrid1"   
        Height="600" Margin="12"  
        ItemsSource="{x:Bind MyViewModel.Customers}" />    

    This is just code samples from Docs and you need to modify them. FYI:

    0 comments No comments

0 additional answers

Sort by: Most helpful