Click here to Skip to main content
Click here to Skip to main content

Generic GridView Using DataTable with Sorting and Paging

, 23 May 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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)

Share

About the Author

yordan_georgiev
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

 
GeneralAgreed. Just posting code with no discourse is a waste of time. Pinmemberdmartini-iam28-May-09 4:20 
GeneralRe: Agreed. Just posting code with no discourse is a waste of time. Pinmemberyordan_georgiev28-May-09 6:04 

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
Web01 | 2.8.141223.1 | Last Updated 23 May 2009
Article Copyright 2009 by yordan_georgiev
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid