Click here to Skip to main content
12,947,539 members (57,277 online)
Click here to Skip to main content
Add your own
alternative version


28 bookmarked
Posted 21 Jul 2005

Custom paging and clientside sorting

, 21 Jul 2005
Rate this:
Please Sign up or sign in to vote.
An article on custom paging and JavaScript sorting in DataGrid.

Sample Image - custompageing.jpg


I have seen a lot of samples on custom paging and client side sorting but they all seem to complicate the issues, by either trying to make them fit all situations or creating them as custom controls. Well, like DataGrids, custom controls almost never fit the layout or functionality I'm working on, so I'm always ending up writing a template and custom functionality. So why scan the net for a control that will not properly fix your problem anyway when a few lines of code can do the same. Microsoft has put a lot of effort in their controls and by using them wisely they can solve a lot of problems.

The solution will fetch only the data from the database needed for the current page in the DataGrid. The JavaScript will sort the DataGrid on the first click on the column header ASC and on second DESC. This is meant to be used as template code so you will properly have to customize it to fulfill your needs. "But that's why you are a programmer, right?".

The JavaScript part has been tested to work in IE 6, FireFox 1.0.4 and Opera 8.0. If you need further documentation or explanation don't hesitate to drop me a mail and I'll update the article.

C# Code

public class CustomPageing : System.Web.UI.Page
  protected System.Web.UI.WebControls.DataGrid DataGrid1;
  // used to set which column number
  // a linkbutton is in. used by javascript
  protected int colNumber = 0;
  // used to set which row to start sorting eg width
  // pager and header it will be nr 2. used by javascript
  protected int rowstart = 2;
  OdbcCommand cmd;
  OdbcDataAdapter ad;
  DataSet data = new DataSet();
  OdbcConnection con = new OdbcConnection("DRIVER={MySQL ODBC" + 
         " 3.51 Driver};SERVER=localhost;DATABASE=minannonce;");

  private void Page_Load(object sender, System.EventArgs e)
      // for simplicity i have kept this in pageload
      // but should be in a search method
      // set virtual count so Pager know how many pages to prepare
      // we keep it in a Session so we only have to do this once pr search
      cmd = new OdbcCommand("Select Count(*) From Advertisment",con);
      Session["Count"]= Convert.ToInt32(cmd.ExecuteScalar());
      DataGrid1.VirtualItemCount = Convert.ToInt32(Session["Count"]);

public void fillGrid()
  // fill grid width the number of advertisments from db
  // this is from a MySql db so rewrite if you use different Db 
  cmd = new OdbcCommand("Select advertismentId, HeadLine," + 
        " Price From Advertisment LIMIT " + 
        DataGrid1.PageSize*DataGrid1.CurrentPageIndex + 
  ad = new OdbcDataAdapter();
  ad.SelectCommand = cmd;
  DataGrid1.DataSource = data;

public void Sort(object sender, System.EventArgs e)
  // add javascript eventhandler to columnheader
              "sort('"+colNumber+"','"+rowstart+ "','" 
              +DataGrid1.ClientID+"'); return false;");

private void DataGrid1_PageIndexChanged(object source, 
        System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
  // pageindex changer
  DataGrid1.CurrentPageIndex = e.NewPageIndex;


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
Denmark Denmark
Im a software developer and MCAD

I love .NET and the pure object oriented and event driven programming philosophy.

You may also be interested in...

Comments and Discussions

QuestionCast Error Pin
MMaannuu9-Apr-07 0:18
memberMMaannuu9-Apr-07 0:18 
GeneralQuery Pin
hemant.kaushal15-Nov-05 0:28
memberhemant.kaushal15-Nov-05 0:28 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170518.1 | Last Updated 22 Jul 2005
Article Copyright 2005 by Wyxlwiis
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid