by
Venkata Koppaka
| Jun 05, 2010
In this post I will talk about how to build tabular data using ADO.NET EntityFramework and sort tabular / grid data using JQuery table sorter plugin. For the sake of demo I will use ASP.NET 4 and .NET Framework 4.
Fire up Visual Studio 2010 and Create a New ASP.NET WebSite. For this demo I will use ADO.NET Entity Framework to get a list of Products from the database.
I will use AdventureWorks Database for the sake of this demo.
Add a new ADO.NET Entity Data Model, select “generate from database”, hit next choose the and add the product’s table
Add a new C# Class and class it Product.cs this class will be in the same namespace as the EntityDataModel and the Product class will extend as a partial class from the class created by ADO.NET EntityFramework. Here is how the class should look like –
| 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | using System.Linq; |
| 4 | using System.Web; |
| 5 | |
| 6 | /// <summary> |
| 7 | /// Summary description for Product |
| 8 | /// </summary> |
| 9 | /// |
| 10 | namespace AdventureWorksModel |
| 11 | { |
| 12 | public partial class Product |
| 13 | { |
| 14 | public IQueryable<Product> GetProducts() |
| 15 | { |
| 16 | AdventureWorksEntities db = new AdventureWorksEntities(); |
| 17 | return db.Products.Take(200); |
| 18 | } |
| 19 | } |
| 20 | } |
We created a method called GetProducts which just takes the top 200 rows in from the Products table. This method will return a List of Products of type IQueryable.
Now add a new ASP.NET WebForm and call it Products.aspx. In Products.aspx drag drop a GridView and set its DataSource as ObjectDataSource, in the GridView tasks choose new DataSource and Choose object as Data Source Type,
From the choose a Business Object Screen Choose the Products Class we just created.
For the select method Choose the GetProducts method we created earlier.
Choose the necessary columns from the gridview, I choose a few columns for this demo. Once you are done with choosing columns your Products.aspx code should look something like this –
| 1 | <asp:GridView ID="gdvProducts" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1"> |
| 2 | <Columns> |
| 3 | <asp:BoundField DataField="ProductID" HeaderText="ProductID" SortExpression="ProductID" /> |
| 4 | <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> |
| 5 | <asp:BoundField DataField="ProductNumber" HeaderText="ProductNumber" SortExpression="ProductNumber" /> |
| 6 | <asp:CheckBoxField DataField="MakeFlag" HeaderText="MakeFlag" SortExpression="MakeFlag" /> |
| 7 | <asp:CheckBoxField DataField="FinishedGoodsFlag" HeaderText="FinishedGoodsFlag" SortExpression="FinishedGoodsFlag" /> |
| 8 | <asp:BoundField DataField="Color" HeaderText="Color" SortExpression="Color" /> |
| 9 | <asp:BoundField DataField="SafetyStockLevel" HeaderText="SafetyStockLevel" SortExpression="SafetyStockLevel" /> |
| 10 | <asp:BoundField DataField="ReorderPoint" HeaderText="ReorderPoint" SortExpression="ReorderPoint" /> |
| 11 | <asp:BoundField DataField="StandardCost" HeaderText="StandardCost" SortExpression="StandardCost" /> |
| 12 | <asp:BoundField DataField="ListPrice" HeaderText="ListPrice" SortExpression="ListPrice" /> |
| 13 | </Columns> |
| 14 | </asp:GridView> |
| 15 | <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetProducts" |
| 16 | TypeName="AdventureWorksModel.Product"></asp:ObjectDataSource> |
Run the application and this is how your products list will show up.
Download JQuery 1.4.1 and tablesorter plugins and them to the scripts folder of the website. Once done add these scripts to the head section of master page.
| 1 | <link href="~/Content/tablsorter.css" rel="stylesheet" type="text/css" /> |
| 2 | <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script> |
| 3 | <script src="Scripts/jquery.tablesorter.js" type="text/javascript"></script> |
In the HTML for gridview now let’s add a CSS class so that we can use it find the table by JQuery.
| 1 | <asp:GridView ID="gdvProducts" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" CssClass="tablesorter"> |
Note the CssClass Property above which is set to tablesorter.
Now add the following script in the products.aspx page –
| 1 | <script type="text/javascript"> |
| 2 | $(document).ready(function () { |
| 3 | |
| 4 | $('.tablesorter').tablesorter(); |
| 5 | |
| 6 | }); |
| 7 | </script> |
Run the application. Your application should be something similar to this –
Notice the sort up and sort down arrows.
Hope this helps.
Cheers,
Venkata