Edit this page

Change PageSize dynamically

Generally, the page size of a RadDataPager can be set by the corresponding PageSize property. However, in some cases you may require it to be changed dynamically in run time.

So, if for example you want to use a RadComboBox for changing the size and you want it defined in the template of the RadDataPager, you need to do a couple of things;

  • Define an IValueConverter responsible for adding items in the RadComboBox depending on the number of data items in RadGridView:

public class CountConverter : IValueConverter
 {
  public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
   var items = (value as DataPagerPresenter).DataContext as SampleDataSource;
   int i = 1;
   List<int> result = new List<int>();
   foreach (var item in items.Collection)
   {
    if (i % 5 == 0)
    {
     result.Add(i);
    }
    i++;
   }
   return result;
  }
  public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
   return value;
  }
 }
Public Class CountConverter
 Implements IValueConverter
 Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object
  Dim items = TryCast(TryCast(value, DataPagerPresenter).DataContext, SampleDataSource)
  Dim i As Integer = 1
  Dim result As New List(Of Integer)()
  For Each item As var In items.Collection
   If i Mod 5 = 0 Then
    result.Add(i)
   End If
   i += 1
  Next
  Return result
 End Function
 Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object
  Return value
 End Function
End Class
  • Predefine the template of the RadDataPager and bind the SelectedValue of the RadComboBox to the PageSize property of the RadDataPager:

<StackPanel Grid.Column="1" Margin="5,0" Orientation="Horizontal" Visibility="{Binding PagerControlsVisibility.TextControlVisibility, RelativeSource={RelativeSource TemplatedParent}}">
          <telerik:RadComboBox ItemsSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource countConverter}}" SelectedValue="{Binding PageSize, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" x:Name="radComboBox" />  
</StackPanel>
  • Set the updates style to the RadDataPager:

<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource SampleDataSource}}">
    <Grid.RowDefinitions>
 <RowDefinition Height="auto"/>
 <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>
    <telerik:RadGridView  x:Name="grid" ColumnWidth="*" ItemsSource="{Binding Collection}" Width="900"/>
    <telerik:RadDataPager Grid.Row="1" Source="{Binding Items, ElementName=grid}" PageSize="10" 
                          IsTotalItemCountFixed="True" 
                          Style="{StaticResource RadDataPagerStyle1}"/>
</Grid>

Once you created all the necessary elements, your application should be displayed as follows:

You may download a full runnable sample project illustrating the approach described in this Code Library.