Click here to Skip to main content
11,805,035 members (63,706 online)
Click here to Skip to main content

GridViewExpand(To be with ORMPPING ActiveReord,sorting,paging)

, 27 Aug 2008 CPOL 10.5K 175 11
Rate this:
Please Sign up or sign in to vote.
GridViewExpand(To be with ORMPPING ActiveReord,sorting,paging)


The GridViewExpand integrated ORMMAPING (ActiveRecord,Nhibernate), sorting and paging function.


ASP.NET used code-behind style to develop.The design pattern seems could be between MODEL1 servlet AND Model 2 Struts (

When we add paging and sorting to GridView, We must to write a lot of similar codes which are too difficult to do auto-test to every GridView. Fatally, GridView of .NET framework do not implement good paging and sorting because that Gridview select all of the data before paging or sorting the data. That influrence the capability when the data is huge. Though we use ORM instead of dataset to build DAO of project clear, we endure the weakless of it still.

I rewrote the GridView Control to make it more efficient and easier to coding when do paging and sorting with ORMapping.

Using the Code

The sample solution is made up by three projects.

GridViewExpandCastleBase build base query by ActiveRecord

GridViewExpand expand the sorting and paging

WebTest shows the usage

The WebTest of the solution base on Person What maps to northWind on MS SqlServer.You may change the connect string in Web.Config to run WebTest.

Write Person.cs map to table Customers in NorthWind.

/// <span class="code-SummaryComment"><summary></span>
/// <span class="code-SummaryComment"></summary></span>
public class Person: ActiveRecordBase<person />
    private string contactName;
       public string ContactName
        get { return contactName; }
        set { contactName = value; }

Then PersonCond is the query condition object for Person Class inherts from BaseCond<T>. PersonCond must override the abstract method GetHQL() to return a HQL string which help to do ActiveRecordCriteriaQuery.

/// <span class="code-SummaryComment"><summary></span>
/// Query condition
/// <span class="code-SummaryComment"></summary></span>
public class PersonCond : BaseCond 

    public PersonCond(Type targetType)
            : base(targetType)
    /// <span class="code-SummaryComment"><summary></span>
    /// return the query HQL
    /// <span class="code-SummaryComment"></summary></span>
    /// <span class="code-SummaryComment"><returns></returns></span>
    public override string getHQL()

        StringBuilder s = new StringBuilder();
        s.Append("from Person as p  where 1=1 ");

        if (id!=null&&id!="")
            s.Append("and  p.Id like '" + id + "%' ");

        return s.ToString();

Then you need just to add GridViewExpand to your toolbox then use it as usual in aspx page. in *.cs file,when you want to full the data to it,you write

protected void Button1_Click(object sender, EventArgs e)
     //Below to fill the query condition
     PersonCond p = new PersonCond(typeof(Person));
     p.Address = TextBoxAddress.Text;
     p.City = TextBoxCity.Text;
     p.ContactName = TextBoxName.Text;
     p.PostalCode = TextBoxPostalCode.Text;
     //Push query condition to the GridViewExpand
     GridView1.CustomPagerSettings.QueryBaseCond = p;

and it will make paging and sorting function automaticaly.


  1. BaseCastle project
    It break up the query to two steps if BaseCond.IsCount is set true. First,query the total count according with the query. Second, query data according with query(query conditon,page index,page size) .
  2. Main Architect of GridViewExpand

    GridViewExpand integrated sorting and paging by hold up OnCommand event.

    Abstract class:ExtendFunction.cs. Paging and Sorting function implement the abstract class with its function on Excute() Event class:event.cs. Both functions add their own event in it to link to the delegate when the GridViewExpand initialize. These class is partitial of GridViewExpand. Property Class. Properties for each function.These class is partitial of GridViewExpand. GridViewExpand.cs.Override some event and methord to add your own to it.

  3. 3.Other

(1) Embed-resource write

[assembly: System.Web.UI.WebResource("GridViewExpand.Resources.Asc.gif", "image/gif")]

tag to add your BMP/GIF to the class as embed-resource.



to use it.

(2)Use below in front of Property

/// <span class="code-SummaryComment"><summary></span>
/// PagerCustom
/// <span class="code-SummaryComment"></summary></span>

So it will be show at visual studio the property window.

Points of Interest

See more for help: Expand architect for GridView:

Creating Custom ASP.NET Controls Roadmap:


Initial release.


What is PersonCond and how do we use it?

Answer: Then PersonCond is the query condition object for Person Class inherts from BaseCond. PersonCond must override the abstract method GetHQL() to return a HQL string which help to do ActiveRecordCriteriaQuery.

BaseCond<T> is an encapsulation of ActiveRecordCriteriaQuery. We can set page index, pageSize and other parameters to query. The class is initial with Generic Class


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

You may also be interested in...

Comments and Discussions

Question[Message Deleted] Pin
Joker_jfxia3-Sep-08 16:38
memberJoker_jfxia3-Sep-08 16:38 
AnswerRe: Error Pin
clarkapp3-Sep-08 22:16
memberclarkapp3-Sep-08 22:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151002.1 | Last Updated 27 Aug 2008
Article Copyright 2008 by clarkapp
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid