Formatting the paging label of a Telerik RadGrid

I was encountering in issue with paging when dealing with a Telerik RadGrid. In our application, we have certain (but not all) records that can have child records. The problem was that the label would say 100 records where there were 75 parent records and 25 child records, and I wanted to just have the parent rows counted, since those were the only ones initially visible. So the label in the paging row said:

100 items in 4 pages

And I wanted it to say:

75 items in 3 pages.

(This is assuming that 25 records were being shown per page.)

Before going any farther, I have to say that the Telerik forums were a great resource. One of the Telerik employees did a great job of answering my questions, without which I probably couldn’t have finished this.

First, I declared a global page counter to hold the parent records:

int pagerRowCount = 0;

In the RadGrid’s NeedDataSource event, I wrote the code to count the number of parent lines. I was able to do this because our CustLineItem object had a property called hasChildRecords to determine if there were child records:

LinePager<CustLineItem> pager = new LinePager<CustLineItem>(Items);

foreach (CustLineItem item in Items)
    if (custLineNum.hasChildRecords == true)

(The LinePager class is declared at public class LinePager <T>, and has an internal property declared as an IList<t> to hold the list of object used for paging. Items is a collection of object populated from the database.)

In the RadGrid’s PreRender event, which is called after the NeedDataSource  event,  I added the code to modify the label:

string pageLabel = pagerRowCount > SizeToDisplay ? "pages" : "page";

GridPagerItem pagerItem = (GridPagerItem)gridLineItems.MasterTableView.GetItems(GridItemType.Pager)[0];
gridLineItems.PagerStyle.PagerTextFormat = "{4} " + pagerRowCount + " items in " + " {1} " + pageLabel;

The SizeToDisplay variable is a global property and represents the number of items to display. It defaults to 25, but can be changed by a drop list which is part of the RadGrid.

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: