display data in a gridview using ado.net EntityDataSource control

EntityDataSource control is a data source control available in ASP.Net. In ASP.Net applications, the EntityDataSource control is used to bind data retrieved from an Entity Data Model to the data bound controls.

In this article, we will learn how to use the EntityDataSource control and display data in a GridView control.

Steps to follow:

Place the Entity Data Source control on a web form. Specify the data source using the Configure Data Source option as shown in below fig.

configure data source
In the second screen of Configure Data Source dialog box, select the fields to be retrieved from the table and click on the finish button.

Now place the GridView control on the web form. Set the GridView control data source property to EntityDataSource1 by using Choose Data Source option of the GridView control.

If we open the source file of web form, you can see the markup generated for gridview and entity data source control. In the below code the DataSourceID of the GridView control is set to EntityDataSource1.
< asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="CustomerId" 
        DataSourceID="EntityDataSource1">
        < Columns>
            < asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            < asp:BoundField DataField="CustomerId" HeaderText="CustomerId" ReadOnly="True" 
                SortExpression="CustomerId" />
            < asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            < asp:BoundField DataField="Address" HeaderText="Address" 
                SortExpression="Address" />
        < /Columns>
    < /asp:GridView>
    < asp:EntityDataSource ID="EntityDataSource1" runat="server" 
        ConnectionString="name=SalesOrderManagementEntities" 
        DefaultContainerName="SalesOrderManagementEntities" EnableDelete="True" 
        EnableInsert="True" EnableUpdate="True" EntitySetName="Customer">
    < /asp:EntityDataSource>

Run the application. The output generated is shown in the figure below.
gridview output