Bind Entity Framework 4 Model to ASP.NET GridView

Posted by: Suprotim Agarwal , on 12/27/2011, in Category Entity Framework
Views: 106672
Abstract: In this article we will see how to bind an Entity Framework model that has been created in a different project, to an ASP.NET GridView.

If you have been following my Entity Framework 4.0 series, we have covered some basics of the What, Why and How of Entity Framework 4.0 and also performed some CRUD operations with it. In this article, we will see how to bind an Entity Data Model to the ASP.NET GridView and display data.

There have been a couple of articles written by others in the past that shows how to bind an Entity Model with an ASP.NET GridView and other controls. Although these articles are valid in their own context, however they do not show you how to create an Entity model independent of the ASP.NET project. What’s different about this article is that we will use a model that has been created separately, i.e. in a different project and then bind this model to an ASP.NET GridView. I will use the same model created in one of my previous articles Create an Entity Framework Model and Use it in Multiple Projects. So let’s get started.

Step 1: Create a Blank ASP.NET Empty Web Site. You can also create the default ASP.NET Web Site, but since it comes with additional files and code, I will go with the blank ASP.NET site option. Make sure .NET Framework 4.0 is selected as the framework

Empty ASP.NET Site 

Once the project is created, right click the project in the Solution Explorer > Add New Item > Web Form > OK.

Step 2: We now have to add the Entity Model that we created earlier. Once you have downloaded the source code, Right click your project in the Solution Explorer > Add Reference > Go to the Browse Tab and browse to the MyEntityModel.dll in the debug/release folder of the project that we created earlier. Adding a reference to the project gives you access to the classes in that project

Step 3: The next step is to add the connection string used in the model. Open the project with the Entity model we created earlier and copy the connecting string from the App.Config file to the web.config, as shown below:

Entity Data Model Conn String 

Copying the connection string gives us the ability to access the metadata of the Model as well as the connection to the data source, i.e. the Northwind database.

Step 4: Having added a reference to the Entity Model and setting the connection string, we are now ready to use the Entity Model in our ASP.NET applicaton. To do so, drag and drop a GridView and EntityDataSource in the Default.aspx page.

Click the smart tag of the EntityDataSource > click on Configure Data Source and a wizard pops up. Choose the ‘Named Connection’ radio button and select ‘NorthwindEntities’ from the dropdown, as shown below. Click Next.

Entity Data Source Named Connection 

In the ‘Configure Data Selection’ form, choose ‘Customers’ in the EntityTypeName and EntityTypeFilter fields. Select some columns from the Customers table as shown below and click Finish

Entity Data Source Configure Data 

Step 5: With our EntityDataSource ready, you just have to configure the GridView to use the EntityDataSource. Click on the smart tag of the GridView and choose the EntityDataSource as the data source. Also enable paging and sorting as shown below.

GridView Entity Data Source 

Now if you run the application now, you will get the following error:

For the EntityDataSource, if the query specifies a projection and paging is enabled, a sort expression must be defined. Either set the OrderBy property or set AutoGenerateOrderByClause to true.

The MSDN documentation describes the reason as “When the AutoGenerateOrderByClause property of the EntityDataSource control is set to true, the control automatically generates an ORDER BY clause from the parameter in the ParameterCollection assigned to the OrderByParameters property. This eliminates the need to explicitly assign an ORDER BY clause to the OrderBy property”

In order to make the sample work, we have to add the AutoGenerateOrderByClause to the EntityDataSouce. Make the following changes in the EntityDataSource, as shown below:

EntityDataSouce AutoGenOrderByClause 

The code shown above automatically generates the ORDER BY clause and uses the DefaultValue attribute of the Parameter element to set the value of the parameter.

That’s it. Run the application and you now have a GridView bound to your independent Entity Model.

GridView EntityModel 

The entire source code of this article can be downloaded over here.

Give a +1 to this article if you think it was well written. Thanks!
Recommended Articles
Suprotim Agarwal, ASP.NET Architecture MVP, MCSD, MCAD, MCDBA, MCSE, is the CEO of A2Z Knowledge Visuals Pvt. He primarily works as an Architect Consultant and provides consultancy on how to design and develop .NET centric database solutions.

Suprotim is the founder and primary contributor to DotNetCurry, DNC .NET Magazine, SQLServerCurry and DevCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls. and is authoring another one at The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal


Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by suresh kumar gundala on Tuesday, June 5, 2012 4:40 AM
Hi Suprotim,

I have been reading all of your EF releated articles and its very good.
I have one doubt in this article. In the above example the entity data source is mapped with only one table i.e.,Customer. Is there a possibility to join multiple tables and map the data to entity data source?
Comment posted by Suresh on Tuesday, June 5, 2012 4:41 AM
Nice article
Comment posted by Mike on Friday, November 2, 2012 10:07 PM
Thanks for the article but it's very frustrating how all these type of articles about binding EF to a Gridview conveniently ignore that many grids need columns from different tables in the object model. For example, if I have a master Orders table and I want to show the associated product category I don't want the value of "CategoryID" (ie: the Foreign Key) showing for my "Category" column in teh grid - I want the Name column in the Category table which is linked to it!  I haven't yet found a single example where anyone takes this further and makes it truly useful - 99.9% of object models contain more than one table but yours and all the other examples singularly fail to show how to bind columns to other tables in the object model.
Comment posted by anil babu on Wednesday, November 28, 2012 3:48 AM
plz give me example display two tables data in a single gridview

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel