How to Adjust the Height of .NET MAUI DataGrid?
In this article, we will show you how to dynamically adjust the height of the .NET MAUI DataGrid based on SfBottomSheet Size Changes.
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 below 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;
}
Take a moment to explore this documentation, where you can find more information about Syncfusion .NET MAUI DataGrid (SfDataGrid) with code examples. Please refer to this link to learn about the essential features of Syncfusion .NET MAUI DataGrid (SfDataGrid).
Conclusion
I hope you enjoyed learning about how to adjust the height on .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 for configuration specifications. You can also explore our .NET MAUI DataGrid example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!