Archive for November, 2011

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)
        pagerRowCount++;
}

(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;
gridLineItems.Rebind();

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.

Advertisements

Leave a comment

Half-highlighted rows in a Telerik RadGrid

I worked on an issue where, inside a Telerik RadGrid control, we changed the background color  of rows that had an error status to red. Our normal behavior was selected rows was changing the background color to blue.  The problem was that when selecting one of these rows in error, the line would split – half would be red, and half would be blue. Not attractive.

Blessed be the Telerik forums, for they led me to write this little snippet of code.

<style type="text/css">
    div.RadGrid_Windows7 .rgSelectedRow  
    {
        background: none #D6EBFF;
    }
 </style>

For the longest time, I couldn’t get the issue working, but as the blog post mentions, I put the “div” in front of the style, and that was the key. Once I did that, the style had precedence and it worked!

Leave a comment