|
1 | | -# How-to-dynamically-adjust-the-height-of-the-SfDataGrid-based-on-SfBottomSheet-Size-Changes |
2 | | -This demo shows how to dynamically adjust the height of the SfDataGrid based on SfBottomSheet Size Changes ? |
| 1 | +# How to dynamically adjust the height of the SfDataGrid based on SfBottomSheet Size Changes ? |
| 2 | +In this article, we will show you how to dynamically adjust the height of the [.NET MAUI DataGrid](https://www.syncfusion.com/maui-controls/maui-datagrid) based on SfBottomSheet Size Changes. |
| 3 | + |
| 4 | +## Xaml |
| 5 | +``` |
| 6 | + <ContentPage.BindingContext> |
| 7 | + <local:EmployeeViewModel x:Name="viewModel" /> |
| 8 | + </ContentPage.BindingContext> |
| 9 | +
|
| 10 | + <Grid> |
| 11 | + <Grid> |
| 12 | + <Button Text="ShowBottomSheet" HorizontalOptions="Center" VerticalOptions="Center" |
| 13 | + Clicked="Button_Clicked" /> |
| 14 | + </Grid> |
| 15 | + <!-- Bottom Sheet Section --> |
| 16 | + <bottomSheet:SfBottomSheet x:Name="bottomSheet" |
| 17 | + HalfExpandedRatio="0.55" |
| 18 | + CollapsedHeight="100" |
| 19 | + AllowedState="All" |
| 20 | + CornerRadius="15, 15, 0, 0"> |
| 21 | + <bottomSheet:SfBottomSheet.BottomSheetContent> |
| 22 | + <!--Add your content here--> |
| 23 | + <Grid RowDefinitions="*,48" x:Name="gridLayout" |
| 24 | + ColumnDefinitions="*" |
| 25 | + Margin="10,-5,10,10" > |
| 26 | +
|
| 27 | + <StackLayout x:Name="hslDataGrid" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" |
| 28 | + Grid.Row="0"> |
| 29 | + <datagrid:SfDataGrid AutoGenerateColumnsMode="None" |
| 30 | + GridLinesVisibility="Both" |
| 31 | + HeaderGridLinesVisibility="Both" |
| 32 | + x:Name="dataGrid" |
| 33 | + Margin="0,0,0,0" |
| 34 | + ItemsSource="{Binding Employees}" |
| 35 | + MinimumHeightRequest="0" |
| 36 | + SelectionMode="Single" |
| 37 | + NavigationMode="Cell" |
| 38 | + ColumnWidthMode="Auto" |
| 39 | + HeaderRowHeight="42" |
| 40 | + VerticalOptions="FillAndExpand" |
| 41 | + HorizontalOptions="FillAndExpand" |
| 42 | + RowHeight="42"> |
| 43 | + <datagrid:SfDataGrid.Columns> |
| 44 | + <datagrid:DataGridTextColumn HeaderText="Employee Name" |
| 45 | + MappingName="Name" /> |
| 46 | + <datagrid:DataGridNumericColumn HeaderText="Employee ID" |
| 47 | + Format="#" |
| 48 | + MappingName="EmployeeID" /> |
| 49 | + <datagrid:DataGridTextColumn HeaderText="Designation" |
| 50 | + MappingName="Title" /> |
| 51 | + <datagrid:DataGridTextColumn HeaderText="Gender" |
| 52 | + MappingName="Gender" /> |
| 53 | + <datagrid:DataGridTextColumn HeaderText="Marital Status" |
| 54 | + MappingName="MaritalStatus" /> |
| 55 | +
|
| 56 | + </datagrid:SfDataGrid.Columns> |
| 57 | + </datagrid:SfDataGrid> |
| 58 | + </StackLayout> |
| 59 | + <StackLayout Grid.Row="1"> |
| 60 | + <buttons:SfButton x:Name="btnSelCon" |
| 61 | + Text="Click Me" |
| 62 | + Clicked="BtnSelCon_OnClicked" |
| 63 | + HorizontalTextAlignment="Center" |
| 64 | + Margin="0,15,0,0" |
| 65 | + Padding="1" |
| 66 | + HeightRequest="30" |
| 67 | + WidthRequest="85" /> |
| 68 | + </StackLayout> |
| 69 | + </Grid> |
| 70 | + </bottomSheet:SfBottomSheet.BottomSheetContent> |
| 71 | + </bottomSheet:SfBottomSheet> |
| 72 | + <!-- Bottom Sheet Section --> |
| 73 | + </Grid> |
| 74 | +``` |
| 75 | + |
| 76 | +## Xaml.cs |
| 77 | +The code below demonstrates how to dynamically adjust the height of the `SfDataGrid` based on `SfBottomSheet` size changes. |
| 78 | +``` |
| 79 | +private void Button_Clicked(object sender, EventArgs e) |
| 80 | +{ |
| 81 | + double screenHeight = DeviceDisplay.MainDisplayInfo.Height / DeviceDisplay.MainDisplayInfo.Density; |
| 82 | +
|
| 83 | + double ratio = CalculateExpandRatio(bottomSheet.BottomSheetContent, screenHeight); |
| 84 | +
|
| 85 | + bottomSheet.HalfExpandedRatio = ratio; |
| 86 | +
|
| 87 | + bottomSheet.Show(); |
| 88 | +} |
| 89 | +
|
| 90 | +public double CalculateExpandRatio(View bottomSheetContent, double screenHeight) |
| 91 | +{ |
| 92 | +
|
| 93 | + double contentHeight = 0; |
| 94 | +
|
| 95 | + contentHeight = bottomSheetContent.Measure(double.PositiveInfinity, double.PositiveInfinity).Height; |
| 96 | +
|
| 97 | + // Define minimum and maximum ratios |
| 98 | +
|
| 99 | + double minRatio = 0.4; |
| 100 | +
|
| 101 | + double maxRatio = 0.9; |
| 102 | +
|
| 103 | +
|
| 104 | + // Calculate the ratio based on content height relative to screen height |
| 105 | +
|
| 106 | + double calculatedRatio = contentHeight / screenHeight; |
| 107 | +
|
| 108 | +
|
| 109 | + // Ensure ratio stays within platform-specific bounds |
| 110 | +
|
| 111 | + double finalRatio = Math.Clamp(calculatedRatio, minRatio, maxRatio); |
| 112 | +
|
| 113 | + return finalRatio; |
| 114 | +
|
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +<img src="https://support.syncfusion.com/kb/agent/attachment/inline?token=eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjM1OTA1Iiwib3JnaWQiOiIzIiwiaXNzIjoic3VwcG9ydC5zeW5jZnVzaW9uLmNvbSJ9.tDdkNyWb5kOmFdJ3Ek2jdo12AJV6OkAc-vPgYBDFaec" width=800/> |
| 119 | + |
| 120 | +[View sample in GitHub](https://github.com/SyncfusionExamples/How-to-dynamically-adjust-the-height-of-the-SfDataGrid-based-on-SfBottomSheet-Size-Changes) |
| 121 | + |
| 122 | +Take a moment to explore this [documentation](https://help.syncfusion.com/maui/datagrid/overview), where you can find more information about Syncfusion .NET MAUI DataGrid (SfDataGrid) with code examples. Please refer to this [link](https://www.syncfusion.com/maui-controls/maui-datagrid) to learn about the essential features of Syncfusion .NET MAUI DataGrid (SfDataGrid). |
| 123 | + |
| 124 | +##### Conclusion |
| 125 | + |
| 126 | +I hope you enjoyed learning about how to dynamically adjust the height of the `SfDataGrid` based on `SfBottomSheet` size changes. |
| 127 | + |
| 128 | +You can refer to our [.NET MAUI DataGrid’s feature tour](https://www.syncfusion.com/maui-controls/maui-datagrid) page to learn about its other groundbreaking feature representations. You can also explore our [.NET MAUI DataGrid Documentation](https://help.syncfusion.com/maui/datagrid/getting-started) to understand how to present and manipulate data. |
| 129 | +For current customers, you can check out our .NET MAUI components on the [License and Downloads](https://www.syncfusion.com/sales/teamlicense) page. If you are new to Syncfusion, you can try our 30-day [free trial](https://www.syncfusion.com/downloads/maui) to explore our .NET MAUI DataGrid and other .NET MAUI components. |
| 130 | + |
| 131 | +If you have any queries or require clarifications, please let us know in the comments below. You can also contact us through our [support forums](https://www.syncfusion.com/forums), [Direct-Trac](https://support.syncfusion.com/create) or [feedback portal](https://www.syncfusion.com/feedback/maui?control=sfdatagrid), or the feedback portal. We are always happy to assist you! |
0 commit comments