Wednesday 20 June 2012

Customizing items of RadGrid page size combo based on total records

Telerik provides the easy to use RadGrid control with inbuilt paging feature.

By default paging combo includes 10, 20 and 50 page sizes, regardless of number of records displayed in the grid (i.e. if records counts to 11 then also page size combo displays 10, 20 and 50).

If we want to customize the page size combo items to show

  •         Records count > 10 then page size items should show 10 and 20
  •         Records count >= 20 then page size items should show 10, 20 and 50

In order to achieve the above functionality, we will be using RadGrid ItemCreated and ItemEvent events, refer the below code.
private int totalItemCount;

protected void RadGrid1_ItemEvent(object sender, GridItemEventArgs e)
{
        if (e.EventInfo is GridInitializePagerItem)
        {
            totalItemCount = (e.EventInfo as GridInitializePagerItem).PagingManager.DataSourceCount;
        }
}

protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
        if (e.Item is GridPagerItem)
        {
            GridPagerItem pagerItem = (GridPagerItem)e.Item;
            RadComboBox pageSizeCombo = (RadComboBox)pagerItem.FindControl("PageSizeComboBox");

            for (int i = 0; i < pageSizeCombo.Items.Count; i++)
            {
                RadComboBoxItem item = pageSizeCombo.Items[i];

                if (Convert.ToInt32(item.Value) > totalItemCount)
                {
                    i--;
                    pageSizeCombo.Items.Remove(item.Index);
                }
            }

            if (totalItemCount != Convert.ToInt32(pageSizeCombo.Items[pageSizeCombo.Items.Count - 1].Value))
            {
                RadComboBoxItem item = new RadComboBoxItem();
                item = new RadComboBoxItem(totalItemCount.ToString(), totalItemCount.ToString());
                item.Attributes.Add("ownerTableViewId", e.Item.OwnerTableView.ClientID);
                pageSizeCombo.Items.Add(item);
            }
// If you lost selected value in Pagesize combo
//pageSizeCombo.FindItemByValue(e.Item.OwnerTableView.PageSize.ToString()).Selected = true;

        }

}
Displaying page combo always regardless of number of records, refer below code
<telerik:radgrid allowpaging="true" id="RadGrid1" runat="server">
    <mastertableview>
         <pagerstyle alwaysvisible="true"></pagerstyle>
              . . . . 
              . . . . 
    </mastertableview>
</telerik:radgrid>
Showing "Show All" option in page size combo, refer below combo
private int totalItemCount;
protected void RadGrid1_ItemEvent(object sender, GridItemEventArgs e)
{
        if (e.EventInfo is GridInitializePagerItem)
        {
            totalItemCount = (e.EventInfo as GridInitializePagerItem).PagingManager.DataSourceCount;
        }
}

protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
        if (e.Item is GridPagerItem)
        {   
            GridPagerItem pagerItem = (GridPagerItem)e.Item;
            RadComboBox PageSizeCombo = (RadComboBox)pagerItem.FindControl("PageSizeComboBox");
            RadComboBoxItem item1 = new RadComboBoxItem();
            item1 = new RadComboBoxItem("Show All", totalItemCount.ToString());
            item1.Attributes.Add("ownerTableViewId", e.Item.OwnerTableView.ClientID);
            PageSizeCombo.Items.Add(item1);

        }
}

5 comments:

  1. The page size which we have selected (10)is changing back to the default one(here 5) in radgrid.
    I can't changed page size in radgrid of pagesizecombobox.
    Please help me!

    ReplyDelete
  2. Have you tried with below code snippet.

    // If you lost selected value in Pagesize combo
    //pageSizeCombo.FindItemByValue(e.Item.OwnerTableView.PageSize.ToString()).Selected = true;

    ReplyDelete
  3. How do we default the PageSizeComboBox to "Show All", thus making the initial default view to showing all rows?

    ReplyDelete
  4. On prerender method you have to reset the pagesize of radgrid and then rebind the radgrid.

    ReplyDelete