A Working Sample [^]
Download the Code [^] -
Updated 3/3/2002
The code for the Pageable/Sortable DataGrid was written by Dino Esposito and
appeared in the October 2001 issue of MSDN's 'Cutting Edge' article titled 'Build
a Variety of Custom Controls Based on the DataGrid Control '. The original
article and code can be found
here [^]. I
suggest that you read his article to get an understanding of how the code really
works.
One of the changes that we made was to automatically remove all the columns
from the footer and add an asp label. The label has a public property that sets
the label's text property. We use this in our sample for the Page Summary. In
the OnItemCreated function, I added the following:
if (itemType == ListItemType.Footer) // If this is the footer
{
int h = e.Item.Cells.Count;
for (int t= h-1;t>0;t--) e.Item.Cells.RemoveAt(t);
e.Item.Cells[0].ColumnSpan = h;
e.Item.Cells[0].Controls.Add(lblFooter);
}
The Show All Records check box in our sample is set to AutoPostBack and
merely calls the BindGrid() function where the records per page are set to the
total number of records included in the query. It is nice with a fast connection
and a small dataset - remove if you are going to return 1000s of records or have
a slow connection. The four paging buttons use code that appears in the
GotDotNet paging samples. I always liked the idea of a first/last button. Our
datagrid was then wrapped into a set of <DIV> tags. A quick cut/paste and it
fits right into a new page.
In the code behind page, there are short seven functions that we use to do
all the work of filling, sorting and paging the datagrid. Again they are
arranged so that a cut/paste operation will move them to a new page with little
effort. In fact, the demo project in the download was created in about 5
minutes.
The project contains one page that contains a datagrid bound to our 'Quote for
Today' web service - it will always pull the top 7 records and is set to show 5
records per page. The grid will have 2 pages and all features (sorting, paging,
show all, etc) should work. Tying it to our web service means no connection
strings to worry about in the sample.
Below the datagrid we have also include a second Div tag that contains the
client logic for the to call the 'Quote for Today' function.
| Files Included |
Description of Included File |
| DataGrid.Cs |
The DataGrid Class |
| WebForm1.aspx |
The main .aspx page for this project |
| WebForm1.aspx.cs |
The main .aspx.cs page - most of the code has comments
|
| yZWebService.cs |
The proxy class for the 'Quote for Today' web service
|
Top 