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
, just quickly download and run the code and read later or stop reading. For the patient readers, let us start with the:
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
DataSet it has been given as
- 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
- 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"];
gv.AllowSorting = true;
gv.ShowResultSummary = true;
gv.PageSize = 3;
gv.AllowPaging = true;
gv.AutoGenerateColumns = true;
gv.DataBind ( );
panGvHolder.Controls.Add ( gv );
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.
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.