I want to add "ALL" option to my RadGrid Page Size combo. I want the options 50,100,150,ALL. I know we can customize the page size option. However when I am adding the "ALL" option, it is returning an error "Input string was not in correct format". The requirement is when selecting the "ALL" option I want to show all the records in the Grid.
Please advise!
Thank you,
Asok
36 Answers, 1 is accepted
I have tried the same scenario and that worked fine at my end. Here I am pasting my code please make a double check with your code.
C#:
protected
void
rad_ItemCreated(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridPagerItem)
{
GridPagerItem pager = (GridPagerItem)e.Item;
RadComboBox PageSizeComboBox = (RadComboBox)pager.FindControl(
"PageSizeComboBox"
);
RadComboBoxItem ComboItem =
new
RadComboBoxItem(
"All"
);
PageSizeComboBox.Items.Insert(0, ComboItem);
PageSizeComboBox.AutoPostBack =
true
;
PageSizeComboBox.SelectedIndexChanged +=
new
RadComboBoxSelectedIndexChangedEventHandler(PageSizeComboBox_SelectedIndexChanged);
}
}
void
PageSizeComboBox_SelectedIndexChanged(
object
sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
//Handle the event
}
Thanks,
Shinu.
For your sample, the PageSizeComboBox_SelectedIndexChanged is not getting fired.
Any suggestion?
Thanks,
-levs
I tried to recreate the issue but no aval. I hope the following approach helps you to solve the issue.
How to Add "Show All" option in RadGrid Paging.
Thanks,
Shinu.
Your solution
protected
void
LineItems_ItemEvent(
object
sender, GridItemEventArgs e)
{
if
(e.EventInfo
is
GridInitializePagerItem)
{
TotalItemCount = (e.EventInfo
as
GridInitializePagerItem).PagingManager.DataSourceCount;
}
}
protected
void
LineItems_ItemCreated(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridPagerItem)
{
GridPagerItem pagerItem = (GridPagerItem)e.Item;
RadComboBox pageSizeComboBox = (RadComboBox)pagerItem.FindControl(
"PageSizeComboBox"
);
RadComboBoxItem allItem =
new
RadComboBoxItem();
allItem =
new
RadComboBoxItem(
"All"
, TotalItemCount.ToString());
allItem.Attributes.Add(
"ownerTableViewId"
, e.Item.OwnerTableView.ClientID);
pageSizeComboBox.Items.Add(allItem);
}
}
Can you tell how to fix it?
Thanks,
-Stan
Please try to set the current pagesize as the selected value like this:
pageSizeComboBox .Items.FindItemByValue(grid.PageSize.ToString()).Selected =
true
;
All the best,
Maria Ilieva
the Telerik team
Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>
--> On page load, the pager combo box shows '10, 20, 50, All'. Great!
--> On selection of 'All' the pager adds an additional item and selects it.. '10, 20, 50, 68, All'
--> On my Grid, there is a RadComboBox Filter that I use to filter the results by state. So, after a user 'views all' for say, 'AK', the pager box says '68' instead of 'All', which means when I select another state (i.e. 'AL') I see the first 68 results, instead of seeing 'All'.
To make this make more sense, let me put it this way:
Using the provided code, when I select the 'All' option, an additional list item is added to the RadComboBox 'PageSizeComboBox', with a value of the # of records for the current data source. So instead of the 'All' option being selected, a new list item is automatically added. This is not the behavior we want, or expect.
What I want, is simply, an 'All' option, that when selected, persists as the selected option, and continues to return all results, even after the filter has been changed.
Code I'm using is:
private int TotalItemCount;
protected void rgRecords_ItemCreated(object sender, 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("All", TotalItemCount.ToString());
item1.Attributes.Add("ownerTableViewId", e.Item.OwnerTableView.ClientID);
PageSizeCombo.Items.Add(item1);
PageSizeCombo.Items.FindItemByValue(rgRecords.PageSize.ToString()).Selected = true;
}
}
protected void rgRecords_ItemEvent(object sender, GridItemEventArgs e)
{
if (e.EventInfo is GridInitializePagerItem)
{
TotalItemCount = (e.EventInfo as GridInitializePagerItem).PagingManager.DataSourceCount;
}
}
Try to set the pagesize for "All" to int.MaxValue as described in the forum thread below and verify of this helps:
http://www.telerik.com/community/forums/aspnet-ajax/grid/show-all-option-on-the-pager.aspx
Greetings,
Maria Ilieva
the Telerik team
Great. Now the only problem left is that, after selecting the 'All' option, the combo box adds a new list item, with the value '2147483647'. Is there not a way for an user to select 'All' and for that option to stay persisted through page use? I do not understand why the option 'All' when selected, always adds a new list item to the drop down, instead of staying selected.
Thanks, -B
If anybody does have an answer, or anyone representing Telerik that'd like to keep a current and paid subscriber, sure I'm all ears. However, I only ask that you only reply with a SOLUTION to this problem since I don't have time to keep trying things that don't work.
Again, my problem is:
I have a select box with 10, 20, 50 and [All]. I am adding the all option by using this code:
protected void rgRecords_ItemCreated(object sender, GridItemEventArgs e)
{
if (e.Item is GridPagerItem)
{
RadComboBox combo = (e.Item as GridPagerItem).FindControl("PageSizeComboBox") as RadComboBox;
RadComboBoxItem item = new RadComboBoxItem("All", int.MaxValue.ToString());
item.Attributes.Add("ownerTableViewId", e.Item.OwnerTableView.ClientID);
combo.Items.Add(item);
combo.Items.FindItemByValue(rgRecords.PageSize.ToString()).Selected = true;
}
}
In any web browser (FireFox, Opera, IE, Chrome), when the user selects the 'All' option, a 5th option is added to the drop down list with a value of '2147483647' (int.MaxValue). Not what we want. We want the user to select 'All' and all records to show, and that the drop down list remains unmodified... So when the user selects 'All', the drop down list should contain '10, 20, 50, All' and 'All' should stay selected. Please, no more examples that create page artifacts.
Thanks! -B
Please, begging for a sample project with a RadGrid, that has an 'All' option, that when selected stays selected.
Find attached a sample runnable application which works correctly in all browsers wilt the latest official Q2 2011 SP1 release. Test it on your side and let me know if it works for you.
Best wishes,
Maria Ilieva
the Telerik team
if (e.Item is GridPagerItem)
{
RadComboBox combo = (e.Item as GridPagerItem).FindControl("PageSizeComboBox") as RadComboBox;
RadComboBoxItem item = new RadComboBoxItem("All", int.MaxValue.ToString());
item.Attributes.Add("ownerTableViewId", e.Item.OwnerTableView.ClientID);
combo.Items.Add(item);
if (rgRecords.PageSize == int.MaxValue)
{
combo.Items.FindItemByText("All").Selected = true;
}
else
{
combo.Items.FindItemByValue(rgRecords.PageSize.ToString()).Selected = true;
}
RadComboBoxItem extraItem = combo.Items.FindItemByText(int.MaxValue.ToString());
if (extraItem != null)
{
combo.Items.Remove(extraItem);
}
}
the only thing I added was setting the pager style of the MasterTableView to "allways visible", otherwise the pager disappears as soon as you select "all".
When I select option All, pager control display "8 items in 0 pages". Pager control should display "8 items in 1 pages".
Please check below link.
http://jayeshgoyani.blogspot.in/2012/06/customizing-items-of-radgrid-page-size.html
Thanks,
Jayesh Goyani
Hi Pramit...
I.E. 1 Pages is not proper English, it should be 1 Page. To fix your issue, use Page_PreRender (if you only have a BOTTOM pager, you do not have to do this in the Page_PreRender event. However, if you have both a TOP and BOTTOM pager, then this code must be here, since when the TOP pager is created in the RadGrid_ItemCreated event, it does not know how many items there are in the grid. So you MUST do this after the grid has been created.)
protected
void
Page_PreRender(
object
sender, EventArgs e)
{
//Fix page count in pager
if
(rgRecords.MasterTableView.Items.Count == 0)
{
rgRecords.MasterTableView.PagerStyle.PagerTextFormat =
"{4}<strong>0</strong> items in <strong>0</strong> pages"
;
}
else
if
(rgRecords.MasterTableView.Items.Count == 1)
{
rgRecords.MasterTableView.PagerStyle.PagerTextFormat =
"{4}<strong>{5}</strong> item in <strong>1</strong> page"
;
}
else
if
(rgRecords.PageSize ==
int
.MaxValue || rgRecords.PageCount <= 1)
{
rgRecords.MasterTableView.PagerStyle.PagerTextFormat =
"{4}<strong>{5}</strong> items in <strong>1</strong> page"
;
}
else
{
rgRecords.MasterTableView.PagerStyle.PagerTextFormat =
"{4}<strong>{5}</strong> items in <strong>{1}</strong> pages"
;
}
rgRecords.Rebind();
//Must rebind for changes to take effect.
}
Thanks for your reply.
I have only Bottom pager. Where I should right code to show correct english in pager.
Thanks
Pramit
After adding the item into combobox it should be selected. You have one line missing in your code.
pageSizeCombo.FindItemByValue(e.Item.OwnerTableView.PageSize.ToString()).Selected = true;
Initially I have the same code then I change my code to use int.MaxValue for "All" option. Problem in this code is, When "All" option is selected in pager and user Add/Delete any record in grid. It will give to runtime error. e.Item.OwnerTableView.PageSize is not increase/decrease and above line thru runtime error.
I solve this issue by adding if statement
if (rgRecords.PageSize == int.MaxValue)
{
pageSizeCombo.Items.FindItemByText("All").Selected = true;
}
else
{
pageSizeCombo.Items.FindItemByValue(e.Item.OwnerTableView.PageSize.ToString()).Selected = true;
}
But Now problem is, When I add any item in grid, It Add item in next page, despite "All" option is selected in PagerComboBox.
Thanks
Pramit
Hi Pramit,
Unless you are opposed to using Page_PreRender, I would still use that same method. Full code listing to add the All option, and to have the pager display the correct tense for # of items and # of pages:
ASPX:
<
telerik:RadGrid
ID
=
"rgRecords"
runat
=
"server"
AllowSorting
=
"true"
AllowPaging
=
"true"
AutoGenerateColumns
=
"false"
Width
=
"100%"
PageSize
=
"20"
Skin
=
"WebBlue"
OnNeedDataSource
=
"rgRecords_NeedDataSource"
OnItemCreated
=
"rgRecords_ItemCreated"
>
<
MasterTableView
CommandItemDisplay
=
"None"
>
<
NoRecordsTemplate
>
</
NoRecordsTemplate
>
<
Columns
>
</
Columns
>
<
PagerStyle
AlwaysVisible
=
"true"
Mode
=
"NextPrevAndNumeric"
Position
=
"TopAndBottom"
/>
</
MasterTableView
>
</
telerik:RadGrid
>
C#:
protected
void
Page_Load(
object
sender, EventArgs e)
{
Page.PreRender +=
new
EventHandler(Page_PreRender);
}
protected
void
Page_PreRender(
object
sender, EventArgs e)
{
//Fix page count in pager
if
(rgRecords.MasterTableView.Items.Count == 0)
{
rgRecords.MasterTableView.PagerStyle.PagerTextFormat =
"{4}<strong>0</strong> items in <strong>0</strong> pages"
;
}
else
if
(rgRecords.MasterTableView.Items.Count == 1)
{
rgRecords.MasterTableView.PagerStyle.PagerTextFormat =
"{4}<strong>{5}</strong> item in <strong>1</strong> page"
;
}
else
if
(rgRecords.PageSize ==
int
.MaxValue || rgRecords.PageCount <= 1)
{
rgRecords.MasterTableView.PagerStyle.PagerTextFormat =
"{4}<strong>{5}</strong> items in <strong>1</strong> page"
;
}
else
{
rgRecords.MasterTableView.PagerStyle.PagerTextFormat =
"{4}<strong>{5}</strong> items in <strong>{1}</strong> pages"
;
}
rgRecords.Rebind();
//Must rebind for changes to take effect.
}
protected
void
rgRecords_ItemCreated(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridPagerItem)
{
GridPagerItem gpi = e.Item
as
GridPagerItem;
//Give pager drop down an 'All' option.
RadComboBox combo = gpi.FindControl(
"PageSizeComboBox"
)
as
RadComboBox;
if
(combo !=
null
)
{
RadComboBoxItem item =
new
RadComboBoxItem(
"All"
,
int
.MaxValue.ToString());
item.Attributes.Add(
"ownerTableViewId"
, gpi.OwnerTableView.ClientID);
combo.Items.Add(item);
if
(rgRecords.PageSize ==
int
.MaxValue) { combo.Items.FindItemByText(
"All"
).Selected =
true
; }
else
{ combo.Items.FindItemByValue(rgRecords.PageSize.ToString()).Selected =
true
; }
if
(combo.Items.FindItemByText(
int
.MaxValue.ToString()) !=
null
) { combo.Items.Remove(combo.Items.FindItemByText(
int
.MaxValue.ToString())); }
}
}
}
protected
void
rgRecords_NeedDataSource(
object
sender, EventArgs e)
{
//Set your data source here.
}
There is pretty simple solution to have always "1 pages of 1" for "All" option: use (int.MaxValue-1) instead of (int.MaxValue).
It works at least in Telerik RadControls ASP.NET AJAX 2010Q3.
RadComboBoxItem item = new RadComboBoxItem("All", (int.MaxValue-1).ToString());
WBR,
Sergey Arkhipov.
<
CommandItemTemplate
>
<
div
>
<
asp:Button
CommandName
=
"InitInsert"
CssClass
=
"rgAdd"
RunAt
=
"Server"
/>
<
asp:LinkButton
CommandName
=
"InitInsert"
RunAt
=
"Server"
>New User</
asp:LinkButton
>
</
div
>
</
CommandItemTemplate
>
Load the page, select "All" for the PageSize, then click the insert button. I get an error "Specified argument was out of the range of valid values." in the call stack at GridTableView::set_CurrentPageIndex(), GridTableView::InsertItem().
Is there a solution for this?
Thanks,
Kevin
Find attached the modified version of the application which works correctly for the insert item scenario you are implementing. Test it on your end and let me know if it covers your requirements.
Greetings,
Maria Ilieva
the Telerik team
i have same requirement for rad grid and i am referencing your code ="AllPaging-modified.zip" to add "All" option to page size drop down.
i am able to do this but when i am clicking to Add new record button it gives me exception
exception was-
Specified argument was out of the range of valid values. Parameter name: value
and source of exception
Telerik.Web.UI.GridTableView.set_CurrentPageIndex
I tested again the previously provided application and was not bale to replicate the error you are facing. See the movie below which shows my local tests and let me know if I'm missing something:
http://screencast.com/t/7bWDYiyQWL
Please do let me know if you have applied any modification on the application so that the issue starts to appear.
Regards,
Maria Ilieva
the Telerik team
Thanks for replay ...
I solve issues with page size "all" option by setting GridInsertItemPageIndexAction.ShowItemOnFirstPage.
Thanks for replay ,
now i am facing the new issues with delete - when i am fire delete command of radgrid it gives same exception
exception was-
Specified argument was out of the range of valid values. Parameter name: value
and source of exception
Telerik.Web.UI.GridTableView.set_CurrentPageIndex
I further tested the previously provided project but I'm not able to replicate the mentioned errors on my end. Could I kindly ask you to open regular support ticket and send us your version of the application so we could test is locally and advise you on the problem?
Greetings,
Maria Ilieva
the Telerik team
Hi Maria,
Thanks for replay ,,
sorry i am not getting appropriate forum link to post this problem....
I want to create Rad Grid as user control , so I can easy update my lots of grid's on my application . I have create one user control for rad grid as per my business functionality and various properties ….
Thank you
It will be best to open a new forum thread or a support ticket and post your requirements there. Thus we will be able to easily track the different case you have and provide the most appropriate solution for each specific case.
Greetings,
Maria Ilieva
the Telerik team
hi friends
I got a problem with this, I need to set a pager Dropdownlist with 20,30,40
It's working, the rows are displayed, but the selection in the dropdownlist is back to the first item (20)
any idea ??
greeting !!
if (e.Item is GridPagerItem)
{
// ---------------------------------------------------------
// This is how to add items to the RadGrid Pager ComboBox
// and to adjust the width of the RadGrid Pager dropdown.
// ---------------------------------------------------------
GridPagerItem oGridPagerItem = (GridPagerItem)e.Item;
RadComboBox oRadComboBox = (RadComboBox)oGridPagerItem.FindControl("PageSizeComboBox");
oRadComboBox.Width = Unit.Pixel(50);
oRadComboBox.Items.Clear();
RadComboBoxItem oItem = new RadComboBoxItem("20", "20");
oItem.Attributes.Add("ownerTableViewId", e.Item.OwnerTableView.ClientID);
oRadComboBox.Items.Add(oItem);
oItem = new RadComboBoxItem("30", "30");
oItem.Attributes.Add("ownerTableViewId", e.Item.OwnerTableView.ClientID);
oRadComboBox.Items.Add(oItem);
oItem = new RadComboBoxItem("40", "40");
oItem.Attributes.Add("ownerTableViewId", e.Item.OwnerTableView.ClientID);
oRadComboBox.Items.Add(oItem);
}
Please check commented code / notes in below link.
http://jayeshgoyani.blogspot.in/2012/06/customizing-items-of-radgrid-page-size.html
Thanks,
Jayesh Goyani
protected
void
RadGrid1_ItemCreated(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridPagerItem)
{
// ---------------------------------------------------------
// This is how to add items to the RadGrid Pager ComboBox
// and to adjust the width of the RadGrid Pager dropdown.
// ---------------------------------------------------------
GridPagerItem oGridPagerItem = (GridPagerItem)e.Item;
RadComboBox oRadComboBox = (RadComboBox)oGridPagerItem.FindControl(
"PageSizeComboBox"
);
oRadComboBox.Width = Unit.Pixel(50);
oRadComboBox.Items.Clear();
RadComboBoxItem oItem =
new
RadComboBoxItem(
"20"
,
"20"
);
oItem.Attributes.Add(
"ownerTableViewId"
, e.Item.OwnerTableView.ClientID);
oRadComboBox.Items.Add(oItem);
oItem =
new
RadComboBoxItem(
"30"
,
"30"
);
oItem.Attributes.Add(
"ownerTableViewId"
, e.Item.OwnerTableView.ClientID);
oRadComboBox.Items.Add(oItem);
oItem =
new
RadComboBoxItem(
"40"
,
"40"
);
oItem.Attributes.Add(
"ownerTableViewId"
, e.Item.OwnerTableView.ClientID);
oRadComboBox.Items.Add(oItem);
if
(Session[
"PageSize"
] !=
null
)
{
oRadComboBox.SelectedIndex = Convert.ToInt32(Session[
"PageSize"
].ToString());
}
}
}
protected
void
RadGrid1_PageSizeChanged(
object
sender, GridPageSizeChangedEventArgs e)
{
//RadComboBox ddl = (RadComboBox)sender;
switch
(((RadGrid)sender).PageSize)
{
case
20:
Session[
"PageSize"
] = 1;
break
;
case
30:
Session[
"PageSize"
] = 2;
break
;
case
40:
Session[
"PageSize"
] = 3;
break
;
}
// Session["PageSize"] = ((RadGrid)sender).PageSize;
}
I tested the application on my end again and was not bale to replicate the error you mentioned. I would suggest you to open a separate support ticket and report the issue there so that we could further investigate the problem and track the issue in a timely matter.
Greetings,
Maria Ilieva
the Telerik team
HTML:
<PagerStyle Mode="NextPrevAndNumeric" PageSizeLabelText="Page Size: " PageSizes="50,100,200,2147483647" Position="TopAndBottom" AlwaysVisible="true" />
CodeBehind:
protected void RadGrid1_OnItemCreated(object sender, GridItemEventArgs e)
{
if (e.Item is GridPagerItem)
{
// Change the text of the int.max value to 'All'
RadComboBox combo = (e.Item as GridPagerItem).FindControl("PageSizeComboBox") as RadComboBox;
combo.Items[3].Text = "All";
}
}
Protected
Sub
rgAudit_ItemCreated(sender
As
Object
, e
As
GridItemEventArgs)
If
(
TypeOf
(e.Item)
Is
GridPagerItem)
Then
Dim
pager
As
GridPagerItem = e.Item
Dim
pageSizeCombobox
As
RadComboBox = pager.FindControl(
"PageSizeComboBox"
)
Dim
comboItem
As
RadComboBoxItem =
New
RadComboBoxItem(
"All"
, integer.MaxValue)
comboItem.Attributes.Add(
"ownerTableViewId"
, e.Item.OwnerTableView.ClientID)
pageSizeCombobox.Items.Add(comboItem)
If
pageSizeCombobox.SelectedValue =
Integer
.MaxValue
Then
pageSizeCombobox.Items.FindItemByText(
"All"
).Selected = true
rgAudit.PageSize = pageSizeCombobox.SelectedValue
End
If
Dim
extraItem
As
RadComboBoxItem = pageSizeCombobox.Items.FindItemByText(integer.MaxValue.ToString())
If
Not
extraItem
Is
Nothing
Then
pageSizeCombobox.Items.Remove(extraItem)
End
If
End
If
End
Sub
Here is how I got it to work, this fixes setting the dropdown to all, adding all, and adjusting the list.