|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThe built-in BackgroundThe pager in a Image 1. The default
Creating the extended datagrid controlTo get started, we create a new class inheriting from the class DataGridWithImprovedPager: DataGrid
{
private bool m_DisplayPageNumbers = true;
public DataGridWithImprovedPager()
{
}
}
We override the protected override void OnItemCreated(DataGridItemEventArgs e)
{
switch (e.Item.ItemType)
{
case (ListItemType.Pager):
// Only display paging links if we have more than one page
m_DisplayPageNumbers = (this.PageCount > 1);
// Get the table cell holding the page numbers
TableCell cell = e.Item.Controls[0] as TableCell;
// Hide the cell if we're not displaying page numbers
cell.Visible = m_DisplayPageNumbers;
// Check that we haven't added our numrecords label already
// For some reason, the Pager item gets hit twice.
if (cell.FindControl("lblNumRecords") == null)
{
// Work out how many columns we needs to substract from our colspan
int count = (m_DisplayPageNumbers && this.Columns.Count > 1) ?
this.Columns.Count - 1 : this.Columns.Count;
// Extract the Pager
TableCell pager = (TableCell) e.Item.Controls[0];
// Add Cell to Row to Hold Row Count Label
TableCell newcell = new TableCell();
newcell.ColumnSpan = count;
newcell.HorizontalAlign = HorizontalAlign.Left;
newcell.Style["border-color"] = pager.Style["border-color"];
// Add Label Indicating Row Count
Label lblNumRecords = new Label();
lblNumRecords.ID = "lblNumRecords";
// Create a new column and append it to
// the list if we have more the one column.
// Otherwise, just add our label into the existing column
if (this.Columns.Count > 1)
{
newcell.Controls.Add(lblNumRecords);
// Add Table Cell to Pager
e.Item.Controls.AddAt(0, newcell);
// Subtract from Colspan of Original Pager to Account for New Row
pager.ColumnSpan = pager.ColumnSpan - count;
}
else
{
cell.Controls.AddAt(0, lblNumRecords);
}
}
break;
}
base.OnItemCreated (e);
}
The final step is to override the protected override void OnPreRender(System.EventArgs e)
{
Control c1 = this.Controls[0];
Control c2 = c1.Controls[c1.Controls.Count - 1];
Label lblNumRecords = c2.FindControl("lblNumRecords") as Label;
// Populate the message label with the number of records
lblNumRecords.Text = string.Format("{0} records found.", this.VirtualItemCount);
// If we have pages of data, then update the message to show paging information
if (m_DisplayPageNumbers)
{
lblNumRecords.Text += string.Format(" Currently on page {0} of {1}.",
CurrentPageIndex+1,
PageCount);
}
base.OnPreRender (e);
}
Image 2. Our improved
Points of InterestI had some trouble retrieving the total number of items in the list from within the HistoryThis is my first article, and any suggestions or comments would be appreciated! :-)
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||