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