Thursday, April 12, 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");

                if (totalItemCount > 10 && totalItemCount < 20)
                {
                    pageSizeCombo.Items.Clear();
                    RadComboBoxItem item = new RadComboBoxItem();
                    item = new RadComboBoxItem("10", "10");
                    item.Attributes.Add("ownerTableViewId", e.Item.OwnerTableView.ClientID);
                    pageSizeCombo.Items.Add(item);

                    item = new RadComboBoxItem("20", "20");
                    item.Attributes.Add("ownerTableViewId", e.Item.OwnerTableView.ClientID);
                    pageSizeCombo.Items.Add(item);
                }


                if (totalItemCount >= 20)
                {
                    if (pageSizeCombo.Items.Where(i => i.Value == "50").Count() == 0)
                    {
                        RadComboBoxItem item = new RadComboBoxItem();
                        item = new RadComboBoxItem("50", "50");
                        item.Attributes.Add("ownerTableViewId", e.Item.OwnerTableView.ClientID);
                        pageSizeCombo.Items.Add(item);
                    }
                }

            }
        }

Displaying page combo always regardless of number of records, refer below code

                         
          
                    . . . .
     


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");

              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);

            }
        }
Thus we are done...

Learn by diving in Programming Ocean...
Happy Programming!!!

1 comment:

  1. http://allinworld99.blogspot.in/2014/05/show-hide-column-filed-in-telerik_26.html

    ReplyDelete