Bind DataGrid using Entity framework (EDMX model) in wpf

In this article we will learn how to bind DataGrid using EDMX file in wpf. I already bind dataGrid in window form using EDMX. In both article, i notice that only one difference between both that is xaml file. Lets to start to design this in wpf:

<Window x:Class="SplashSCreen.bindgridpanel"
        Title="bindgridpanel" Height="300" Width="300">
        <DataGrid AutoGenerateColumns="False" ColumnWidth="*" x:Name="dataGrid" HorizontalAlignment="Left" Margin="54,56,0,0" VerticalAlignment="Top" Height="178" Width="173">
                <DataGridTextColumn Binding="{Binding Emp_Id}" Header="Emp_Id" />

                <DataGridTextColumn Binding="{Binding Emp_Name}" Header="Emp_Name" />


Above mentioned code define the binding structure. I have one dataGrid control in the window. x:Name is a property of each control which is define the class instance , through which we can access other property of control in business logic. I have two Text column in dataGrid which take two attribute first one is Binding and another one is Header. Header represent the DataGrid column name and binding define the actual name of database table column's name.

Note : Before going to code behind must to add EDMX file in the project.

using System.Linq;
using System.Windows;

namespace SplashSCreen
    /// <summary>
    /// Interaction logic for bindgridpanel.xaml
    /// </summary>
    public partial class bindgridpanel : Window
        TutorialEntities TE = new TutorialEntities();
        public bindgridpanel()

        private void loadgrid()
            var data = from d in TE.emps select d;
            dataGrid.ItemsSource = data.ToList();

Code generate the following output

Bind DataGrid using Entity framework (EDMX model) in wpf

After add the edmx file in the project , you can access the public property of context class (here i have 'emps'). Through which we can apply operation on dataGrid. 
Bind DataGrid using Entity framework (EDMX model) in wpf Bind DataGrid using Entity framework (EDMX model) in wpf Reviewed by Jacob Lefore on July 28, 2015 Rating: 5

No comments