Skip to main content
A newer version of this page is available. .

Bind the WPF Data Grid to Entity Framework Core Sources

  • 2 minutes to read

This document shows how to bind the GridControl to an Entity Framework (EF) Core source.

Requirements

The Getting Started topic describes how to create a .NET application.

Refer to the .NET/.NET Core Support section for detailed information on .NET/.NET Core support.

Install Entity Framework Core

Go to Tools | NuGet Package Manager | Manage NuGet Packages for Solution.

In the “Browse” tab, search for the ‘microsoft sqlserver’ keyword and install the Microsoft.EntityFrameworkCore.SqlServer package for the current project. Accept the license agreement.

Add SQL NuGet Package

Create a Data Context

For the purpose of this tutorial, use the Countries.db database included in our Demo Center.

Create the Data folder and add the Countries.db database to it from the following folder: C:\Users\Public\Public Documents\DevExpress Demos 21.1\Components\Data

The code snippet below illustrates a data model for the Countries table. The Key attribute specifies the property that identifies a Country entity.

using System;
using System.ComponentModel.DataAnnotations;

public class CountryObject {
    [Key]
    public int Id { get; set; }
    public string Country { get; set; }
    public string Currency { get; set; }
    public string Capital { get; set; }
    public int Population { get; set; }
    public string Languages { get; set; }
}

Create a data context for the Countries table. Derive the data context class from EntityFrameworkCore.DbContext and expose a DbSet<TEntity> property for the data collection.

using Microsoft.EntityFrameworkCore;

public partial class CountriesContext : DbContext {
    public CountriesContext() : base() { }

    public virtual DbSet<CountryObject> Countries { get; set; }
}

Override the DbContext.OnConfiguring method to define a connection string to the database:

using Microsoft.EntityFrameworkCore;

public partial class CountriesContext : DbContext {
    public CountriesContext() : base() { }
    protected override void OnConfiguring(
        DbContextOptionsBuilder optionsBuilder) {
        optionsBuilder.UseSqlite(
            "Data Source=file:Data/Countries.db");
        base.OnConfiguring(optionsBuilder);
    }
    public virtual DbSet<CountryObject> Countries { get; set; }
}

Rebuild the project to compile the generated classes.

Bind the GridControl to Data

Define the window’s data context.

<dx:ThemedWindow
...
xmlns:local="clr-namespace:EntityFrameworkCore">
<dx:ThemedWindow.DataContext>
    <local:CountriesContext/>
</dx:ThemedWindow.DataContext>

Add the GridControl to the project. Specify the ItemsSource property to bind the GridControl to data:

<dxg:GridControl AutoGenerateColumns="AddNew"
            EnableSmartColumnsGeneration="True"
            ItemsSource="{Binding Countries}">
    <dxg:GridControl.View>
        <dxg:TableView />
    </dxg:GridControl.View>
</dxg:GridControl>

Run the application.

EFCore Result