Click here to Skip to main content
15,867,568 members
Articles / Web Development / ASP.NET

Generic GridView Using DataTable with Sorting and Paging

Rate me:
Please Sign up or sign in to vote.
4.11/5 (5 votes)
23 May 2009CPOL5 min read 57.8K   1.2K   17   11
This extended control offers sorting and paging.

Introduction

In my previous article, I presented the XGrid control, which is just an extended GridView, providing generic column sorting and paging.

This article is about the next "version" of the control, which provides better paging functionalities, for those lasting till the middle of the article ... Now for those arriving from Google search for a quick solution of sorting and paging for GridView having DataTable or DataSet as the DataSource, just quickly download and run the code and read later or stop reading. For the patient readers, let us start with the:

Background

Most of the code base for the controls is from Alex Furmanski's Extended GridView with Insert Functionality. The idea of sorting and paging is from Strong Coders. The formatting idea is from Mike Ellison's Formatting AutoGenerateColumns in an ASP.NET Grid. So herewith, I would like to thank those guys for sharing their brilliant ideas, and try to wrap up / bring something additional such as the convenience to have a really generic GridView. For legal disclaimer lovers - I have the bad habit of copy pasting and playing with code at 03:00 AM, so if you find your code or someone else's code, just mail me and I'll fix it ...

For picture lovers - as soon as I get my environment reinstalled (24 h), I will put a basic snapshot of how a simple XGrid looks like.

What do I mean by generic in the context of this article? Why is it so important? Certainly, not C# generics type of "generic", but a GridView with the following features:

  • presents any DataTable / DataSet it has been given as DataSource
  • handles automatic sorting
  • handles automatic paging - you do not have to apply sorting handlers in the page's code-behind (if the data source is DataSet or DataTable)
  • has page size, which can be set dynamically
  • pager has links for FirstPage, PreviousPage, NextPage, LastPage - as well as it generates the -10 ..+10 links from the current page
  • The code provides a basic guideline for how-to format the rows of the XGrid

Using the code

You do not need any database (Northwind, AdventureWorks etc.) for the setup - the data table is generated in the example page's code-behind. Download the project, and open it in VS. Open the website (if you use VS2008, click on the solution file; if you are using an older VS version, Start - Run - devenv.exe - File - Open - Web site) and navigate to the folder you just unpacked to. Press F5. Check the CreateDynamicControls() method - this is the place where you should create your dynamic GridViews (or as a matter of fact all your dynamic controls, but this is an entirely new subject for another article, so if you truly want a dynamic GridView, you would copy paste this page code-behind and use it as a template for your code-behind pages using the dynamic GridViews). Note that the XGrid is simply pasted in the App_Code folder, so as such, you should be able to use it by simply copy pasting it in your project (no separate DLLs in the bin folder required!). Here is a sample code snippet using the control:

C#
Gui.Controls.XGrid gv = new Gui.Controls.XGrid ();
gv.DataSource = Session ["dt"]; //take the datatable from the session 
//or assign it straight
gv.AllowSorting = true; //enable sorting
gv.ShowResultSummary = true; //set the "showing page n out of y number of pages
gv.PageSize = 3; //how many items per page should be presented 
gv.AllowPaging = true; //enable pages
gv.AutoGenerateColumns = true; // do not care about column names
gv.DataBind ( ); //databind otherwise nothing would happen !!!
panGvHolder.Controls.Add ( gv ); //add the control dynamically

Remember that it does not make much sense to display more than 5000 rows of a GridView in a page - regardless of the speed of the connection you will get some performance problems on the client side (the user browser will have problems in rendering ...).

Points of interest and future directions

I noticed that sorting does not work if the column titles have spaces (an exception is raised) - so far I have not needed column titles with spaces, but it would be nice to figure out a fix for this inconvenience.

Currently, I use an external label and textbox to set the size of the GridView with event handlers in the page. If anyone has a working code for those to be integrated into the control, please share the knowledge with the rest of us.

I am currently working on a generic ORM GRUD implementation - by generic, I mean I would like to be able to specify only the Data Source to the GridView and it will be able to call the proper GRUD code (Stored Procedures or generated SQL). This article on MSDN could be a good starting point for the implementation.

A search feature of the upper row would be nice. Ways of specifying drilldowns from database - generically (actually one could be achieved, but it is not generic, thus not for sharing!).

Generic conversation on edit, update - bits -> radio boxes , FK's to dropdowns ... etc.

Comments and feedback are welcome

It would be nice to get some comments even if those are negative. CodeProject is about contribution and sharing - in this spirit, specifying the reason for voting less than 4 is considered a complement. So many times, having Googled for an answer to a particular problem instead of reinventing the wheel, I have been able to quickly start a new project from code originating from here and realize the solution for my problem, which hopefully would happen with this article also, and if it does not, it would be educating for all the readers (and even me) to read about the reasons.

Update

I had to rewrite the whole article - uploads did not function in Chrome, which did mess up the whole article, so I apologize for the lost comments. There were comments about the need for a simple snapshot of the control, and it will be provided as soon as I get my whole environment reinstalled.

License

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


Written By
Web Developer Oxit Oy
Finland Finland
I work in OXIT - a small IT consulting company, which has participated in the building of the most sophisticated IT Systems for several big Finnish and international companies (including Fortune 500 members) and continues to provide highly sophisticated IT Solutions to its customers.

I enjoy designing and implementing software or small scripts in different programming languages.

I am fascinated by the magic of software, which has the power to change the world.

Comments and Discussions

 
QuestionSorting Gridview with list datasource Pin
Xavier Nishanth12-Aug-14 3:45
professionalXavier Nishanth12-Aug-14 3:45 
GeneralMy vote of 1 Pin
kamran pervaiz9-Mar-12 4:35
kamran pervaiz9-Mar-12 4:35 
QuestionCode for achieving it Pin
kamran pervaiz19-Oct-11 22:26
kamran pervaiz19-Oct-11 22:26 
GeneralPaging through data retrieved from a huge table Pin
maximus.dec.meridius13-May-11 11:10
maximus.dec.meridius13-May-11 11:10 
Generalthanks a million great work man Pin
dvd_Besoo5-Dec-09 7:37
dvd_Besoo5-Dec-09 7:37 
GeneralRe: thanks a million great work man Pin
yordan_georgiev6-Dec-09 8:53
yordan_georgiev6-Dec-09 8:53 
GeneralRe: thanks a million great work man Pin
dvd_Besoo6-Dec-09 21:14
dvd_Besoo6-Dec-09 21:14 
GeneralAgreed. Just posting code with no discourse is a waste of time. Pin
dmartini-iam28-May-09 3:20
dmartini-iam28-May-09 3:20 
GeneralRe: Agreed. Just posting code with no discourse is a waste of time. Pin
yordan_georgiev28-May-09 5:04
yordan_georgiev28-May-09 5:04 
GeneralMy vote of 1 Pin
tmeyerpiton25-May-09 3:37
tmeyerpiton25-May-09 3:37 
GeneralThe picture of the control [modified] Pin
yordan_georgiev24-May-09 8:26
yordan_georgiev24-May-09 8:26 
Can be seen here

Btw. it was not mentioned in the article, yet you COULD have as many grids as you want on a page ...

For some reason I cannot edit anymore the article's content ... so here is the picture for those needing it ... Not as much of a good marketing though ; ) At least a better Customer Service

It is one thing to know what to want, second to really want it, third to know how to do it, fourth to be skillful to do it, fifth to actually do it and last but not least to not regret after doing it

modified on Tuesday, May 26, 2009 1:52 AM

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.