Articles in this section
Category / Section

How to Adjust the Height of .NET MAUI DataGrid?

9 mins read

This article demonstrates how to dynamically adjust the height of the .NET MAUI DataGrid based on size changes in the SfBottomSheet. This approach ensures that the DataGrid adapts to the visible space, improving visual adaptability.

Xaml

 <ContentPage.BindingContext>
     <local:EmployeeViewModel x:Name="viewModel" />
 </ContentPage.BindingContext>

 <Grid>
     <Grid>
         <Button Text="ShowBottomSheet" HorizontalOptions="Center" VerticalOptions="Center"
                 Clicked="Button_Clicked" />
     </Grid>
  <!-- Bottom Sheet Section -->
     <bottomSheet:SfBottomSheet x:Name="bottomSheet"
                                HalfExpandedRatio="0.55"
                                CollapsedHeight="100"
                                AllowedState="All"
                                CornerRadius="15, 15, 0, 0">
         <bottomSheet:SfBottomSheet.BottomSheetContent>
             <!--Add your content here-->
         <Grid RowDefinitions="*,48" x:Name="gridLayout"
               ColumnDefinitions="*"
               Margin="10,-5,10,10" >

                 <StackLayout x:Name="hslDataGrid" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"
                                        Grid.Row="0">
                     <datagrid:SfDataGrid AutoGenerateColumnsMode="None"
                                          GridLinesVisibility="Both"
                                          HeaderGridLinesVisibility="Both"
                                          x:Name="dataGrid"
                                          Margin="0,0,0,0"
                                          ItemsSource="{Binding Employees}"
                                          MinimumHeightRequest="0"
                                          SelectionMode="Single"
                                          NavigationMode="Cell"
                                          ColumnWidthMode="Auto"
                                          HeaderRowHeight="42"
                                          VerticalOptions="FillAndExpand"
                                          HorizontalOptions="FillAndExpand"
                                          RowHeight="42">
                         <datagrid:SfDataGrid.Columns>
                             <datagrid:DataGridTextColumn HeaderText="Employee Name"
                                                          MappingName="Name" />
                             <datagrid:DataGridNumericColumn  HeaderText="Employee ID"
                                                              Format="#"
                                                              MappingName="EmployeeID" />
                             <datagrid:DataGridTextColumn  HeaderText="Designation"
                                                           MappingName="Title" />
                             <datagrid:DataGridTextColumn  HeaderText="Gender"
                                                           MappingName="Gender" />
                             <datagrid:DataGridTextColumn  HeaderText="Marital Status"
                                                           MappingName="MaritalStatus" />

                         </datagrid:SfDataGrid.Columns>
                     </datagrid:SfDataGrid>
             </StackLayout>
                 <StackLayout Grid.Row="1">
                     <buttons:SfButton x:Name="btnSelCon"
                                   Text="Click Me"
                                   Clicked="BtnSelCon_OnClicked"
                                   HorizontalTextAlignment="Center"
                                   Margin="0,15,0,0"
                                   Padding="1"
                                   HeightRequest="30"
                                   WidthRequest="85" />
             </StackLayout>
         </Grid>
     </bottomSheet:SfBottomSheet.BottomSheetContent>
 </bottomSheet:SfBottomSheet>
 <!-- Bottom Sheet Section -->
 </Grid>

Xaml.cs
The code demonstrates how to dynamically adjust the height of the SfDataGrid based on SfBottomSheet size changes.

private void Button_Clicked(object sender, EventArgs e)
{
    double screenHeight = DeviceDisplay.MainDisplayInfo.Height / DeviceDisplay.MainDisplayInfo.Density;

    double ratio = CalculateExpandRatio(bottomSheet.BottomSheetContent, screenHeight);

    bottomSheet.HalfExpandedRatio = ratio;

    bottomSheet.Show();
}

public double CalculateExpandRatio(View bottomSheetContent, double screenHeight)
{

    double contentHeight = 0;

    contentHeight = bottomSheetContent.Measure(double.PositiveInfinity, double.PositiveInfinity).Height;

    // Define minimum and maximum ratios

    double minRatio = 0.4;

    double maxRatio = 0.9;


    // Calculate the ratio based on content height relative to screen height

    double calculatedRatio = contentHeight / screenHeight;


    // Ensure ratio stays within platform-specific bounds

    double finalRatio = Math.Clamp(calculatedRatio, minRatio, maxRatio);

    return finalRatio;

}

Output

Download the complete sample from GitHub.

Conclusion
I hope you enjoyed learning how to adjust the height of the .NET MAUI DataGrid.

You can refer to our .NET MAUI DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. Explore our .NET MAUI DataGrid example to understand how to create and manipulate data.

For current customers, check out our components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our other controls.

Please let us know in the comments section if you have any queries or require clarification. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied