Using DLINQ with ASP.NET (Part 2 of my LINQ series)
page 8 of 10
by Scott Guthrie
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 38524/ 133

Step 6: Enable Basic Pagination

The previous step is nice because we can now see the 5 most recent orders for each customer, but the downside is that it has expanded the html height quite a bit.  To make the listing a little cleaner we’ll go ahead and enable paging support on the GridView, set the pagesize to 3 rows per page, and handle the appropriate page event handler in our code-behind to re-bind the Grid as appropriate when users click on the new page index at the bottom of the grid

For completeness, here is what the entire .aspx file looks like with the Gridview with hierarchical binding and paging enabled:

Listing 14


<%@ Page Language="C#" CodeFile="Sample4.aspx.cs" Inherits="Sample4" %>
    <form id="form1" runat="server">
    <h1>Northwind Customers</h1>
    <asp:GridView ID="GridView1" AllowPaging="true" PageSize="3" 
                  AutoGenerateColumns="false" runat="server" 
          <asp:BoundField HeaderText="Customer ID" DataField="CustomerID" />
          <asp:BoundField HeaderText="Name" DataField="CompanyName" />
          <asp:BoundField HeaderText="City" DataField="City" />
          <asp:BoundField HeaderText="State" DataField="Region" />
          <asp:BoundField HeaderText="NumOrders" DataField="NumOrders" />             
          <asp:TemplateField HeaderText="Recent Orders">              
                    <asp:Repeater datasource='<%# Eval("Orders") %>' runat="server">                            
                             <a href="todo"><%# Eval("OrderID") %></a>
                             (<%# Eval("OrderDate", "{0:dd MMM yyyy}")%>)

And here is then the entire code-behind:

Listing 15

using System;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Query;
public partial class Sample4 : System.Web.UI.Page {
    void BindData() {
        string connStr = ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString; 
        Northwind.Northwind db = new Northwind.Northwind(connStr);
        GridView1.DataSource = (from customer in db.Customers
                               where customer.Country == "USA"
                               orderby customer.CompanyName
                               select new {
                                  CustomerID = customer.CustomerID,
                                  CompanyName = customer.CompanyName,
                                  City = customer.City,
                                  Region = customer.Region,
                                  NumOrders = customer.Orders.Count,
                                  LastOrder = customer.Orders.Max(o => o.OrderDate),
                                  Orders = customer.Orders.OrderByDescending(o => o.OrderDate).Take(5)
    protected void Page_Load() {
        if (Page.IsPostBack == false)
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) {
        GridView1.PageIndex = e.NewPageIndex;

And now I have a pageable GridView, showing both relational data and calculated aggregate data, both in a tabular and hierarchical way:

Figure 6

View Entire Article

User Comments

Title: DLINQ Usage   
Name: Subhashini
Date: 2010-12-18 1:36:06 AM
This article solved many of questions about DLINQ.I understand how to do create database structures for DLINQ query using Attribute Based Mapping and through XML Based Mapping.
Title: Helping hand for the DLINQ users   
Name: Tejaswini Jangale-Chaudhari
Date: 2009-04-22 1:02:16 AM
The article is real nice, n helped me a lot to understand binding and pagination. Keep posting such helpful articles :)
Title: Great Article   
Name: Yuna
Date: 2008-07-18 3:42:52 AM
thank you very much, my article help me understand LINQ
Title: Great Article   
Name: Jaykumar Acharya
Date: 2008-06-18 9:33:59 AM
I must say you are my guru of LINQ. These posts really helped me to learn LINQ and DLINQ. It was like a cake walk to learn deep concepts. Please keep posting such blogs for us.

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-05-27 8:19:51 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search