We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Setting Page Size Dynamically based on DataGrid size

Hello,
I like to set the page size based on the DataGrid size, such that no scroll bar appears.

So I set the PageSize to the following values in the Window_Loaded event-handler:
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            for (var tabIndex = MainTabControl.Items.Count - 1; tabIndex >= 0; tabIndex--)
            {
                MainTabControl.SelectedIndex = tabIndex;
                MainTabControl.UpdateLayout();
            }
            dpOrderHistory.PageSize = (int)(dgOrderHistory.ActualHeight / dgOrderHistory.RowHeight);
            dpOrderList.PageSize = (int)(dpOrderList.ActualHeight / dgOrderList.RowHeight);
        }
Here is my XAML-Code:

        <TabControl x:Name="MainTabControl">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="{Binding CMDSelectedTabChanged}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <TabItem Header="Bestellliste" >
                <DockPanel>
                    <syncfusion:SfDataPager x:Name="dpOrderList" Source="{Binding TotalProductList}" PageSize="20"   DockPanel.Dock="Bottom"/>
                    <syncfusion:SfDataGrid x:Name="dgOrderList" ItemsSource="{Binding PagedSource, ElementName=dpOrderList}"  SelectionMode="Single" CellTapped="dgOrderList_CellTapped" AutoGeneratingColumn="dgOrderList_AutoGeneratingColumn" AllowSorting="False">

                    </syncfusion:SfDataGrid>
                </DockPanel>
            </TabItem>
            <TabItem Header="Bestellhistorie">
               
                <DockPanel>
                    <syncfusion:SfDataPager x:Name="dpOrderHistory" Source="{Binding TotalOrderStatusList}" PageSize="20"  DockPanel.Dock="Bottom"/>
                    <syncfusion:SfDataGrid x:Name="dgOrderHistory" ItemsSource="{Binding PagedSource, ElementName=dpOrderHistory}"  SelectionMode="Single" CellTapped="dgOrderHistory_CellTapped" AutoGeneratingColumn="dgOrderHistory_AutoGeneratingColumn" AllowSorting="False" >

                    </syncfusion:SfDataGrid>
                </DockPanel>
            </TabItem>
        </TabControl>

If I run the program, the PageSize is still 20. If i do not specify the value in XAML then no rows are displayed in the DataGrid. However I bound a Label to the PageSize value and it displays 34, but the grid shows only 20 items.

How should I change the code such that the grid displays as much items as possible without scrollbars.

Which Event is the right place to compute the PageSize?


1 Reply

MK Muthukumar Kalyanasundaram Syncfusion Team June 3, 2017 08:52 AM UTC

Hi Backer, 
 
Thank you for contacting Syncfusion support. 
 
We have analyzed your provided code. Please find the response from below table. 
 
 
Query 1: 
If I run the program, the PageSize is still 20. If i do not specify the value in XAML then no rows are displayed in the DataGrid. However I bound a Label to the PageSize value and it displays 34, but the grid shows only 20 items. 
 
As per your requirement, we have checked and prepared the sample. For your reference, we have attached the sample in the below location. In the sample, we have loaded the SfDataPager.PageSize based on SfDataGrid  size as shown like below code

Code snippet: 
private void MainWindow_Loaded(object sender, System.Windows.RoutedEventArgs e) 
{ 
    for (var tabIndex = MainTabControl.Items.Count - 1; tabIndex >= 0; tabIndex--) 
    { 
        MainTabControl.SelectedIndex = tabIndex; 
        MainTabControl.UpdateLayout(); 
    } 
    sfDataPager.PageSize = (int)(dataGrid.ActualHeight / dataGrid.RowHeight) - 1; 
    sfDataPager1.PageSize = (int)(dataGrid1.ActualHeight / dataGrid1.RowHeight) - 1; 
    sfDataPager.MoveToFirstPage(); 
    sfDataPager1.MoveToFirstPage(); 
} 
 
 
Query 2: 
How should I change the code such that the grid displays as much items as possible without scrollbars. 
 
Query 3: 
Which Event is the right place to compute the PageSize? 
 
 
There is no event available to calculate the pagesize in datapager. 
 
 
 
Please let us know if you have any query. 
 
Regards, 
Muthukumar K 


Loader.
Live Chat Icon For mobile
Up arrow icon