Change RadGrid PageSize Dropdown


Introduction:

Telerik’s RadGrid provides in-built functionality for paging. User have to select the number of records from the Page Size combo box (default is 10, 20 and 50) and selected number of records is going to display in RadGrid. But we can also change the Page Size in combo box as per our requirement. Following is the code to do the same:

Code:

protectedvoid grdViewReport_ItemDataBound(object sender, GridItemEventArgs e)

{

if (e.Item isGridPagerItem)

{

RadComboBox PageSizeCombo = (RadComboBox)e.Item.FindControl(“PageSizeComboBox”);

PageSizeCombo.Items.Clear();

PageSizeCombo.Items.Add(newRadComboBoxItem(“100”));

PageSizeCombo.FindItemByText(“100”).Attributes.Add(“ownerTableViewId”, grdViewReport.MasterTableView.ClientID);

PageSizeCombo.Items.Add(newRadComboBoxItem(“200”));

PageSizeCombo.FindItemByText(“200”).Attributes.Add(“ownerTableViewId”, grdViewReport.MasterTableView.ClientID);

PageSizeCombo.Items.Add(newRadComboBoxItem(“500”));

PageSizeCombo.FindItemByText(“500”).Attributes.Add(“ownerTableViewId”, grdViewReport.MasterTableView.ClientID);

PageSizeCombo.FindItemByText(e.Item.OwnerTableView.PageSize.ToString()).Selected = true;

}

}

3 Responses to Change RadGrid PageSize Dropdown

  1. Just a little improvement over the same code above.

    public void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
    if (e.Item is GridPagerItem)
    {
    RadComboBox PageSizeCombo = (RadComboBox)e.Item.FindControl(“PageSizeComboBox”);
    Dictionary items = new Dictionary(){
    // {“10″,”10”},
    // {“20″,”20”},
    // {“50″,”50”},
    {“100″,”100”},
    {“200″,”200”},
    {“All”,”9999999″}
    };
    foreach (var item in items)
    {
    var oItem = new RadComboBoxItem { Text = item.Key, Value = item.Value};
    oItem.Attributes.Add(“ownerTableViewId”, e.Item.OwnerTableView.ClientID);
    PageSizeCombo.Items.Add(oItem);
    }
    PageSizeCombo.FindItemByText(e.Item.OwnerTableView.PageSize.ToString()).Selected = true;
    }
    }

    • Still better

      public void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
      {
      if (e.Item is GridPagerItem)
      {
      RadComboBox PageSizeCombo = (RadComboBox)e.Item.FindControl(“PageSizeComboBox”);
      Dictionary items = new Dictionary(){
      // {“10″,”10”},
      // {“20″,”20”},
      // {“50″,”50”},
      {“100″,”100”},
      {“200″,”200”},
      {“All”, ((GridPagerItem)e.Item).Paging.DataSourceCount.ToString()}
      };
      foreach (var item in items)
      {
      var oItem = new RadComboBoxItem { Text = item.Key, Value = item.Value};
      oItem.Attributes.Add(“ownerTableViewId”, e.Item.OwnerTableView.ClientID);
      PageSizeCombo.Items.Add(oItem);
      }
      PageSizeCombo.FindItemByText(e.Item.OwnerTableView.PageSize.ToString()).Selected = true;
      }
      }

Leave a comment