How to adjust row heights of each row based on their content?
Xamarin.Forms DataGrid allows you to customize the row height of grid rows using the SfDataGrid.QueryRowHeight event. You can auto fit a row based on its content by using the SfDataGridHelpers.GetRowHeight method.
Handled property of the QueryRowHeightEventArgs must be set to true in the QueryRowHeight event for the height changes to take effect.
<sfGrid:SfDataGrid x:Name="dataGrid" AutoGenerateColumns="False" ColumnSizer="Star"> <sfGrid:SfDataGrid.Columns> <sfGrid:GridTextColumn MappingName="OrderId" /> <sfGrid:GridTextColumn MappingName="CustomerId" /> <sfGrid:GridTemplateColumn MappingName="CustomerQuery"> <sfGrid:GridTemplateColumn.CellTemplate> <DataTemplate> <Label Text="{Binding CustomerQuery}" /> </DataTemplate> </sfGrid:GridTemplateColumn.CellTemplate> </sfGrid:GridTemplateColumn> <sfGrid:GridTextColumn MappingName="Country" /> </sfGrid:SfDataGrid.Columns> </sfGrid:SfDataGrid>
private ViewModel viewModel; public MainPage() { InitializeComponent(); viewModel = new ViewModel(); dataGrid.ItemsSource = viewModel.OrdersInfo; dataGrid.QueryRowHeight += DataGrid_QueryRowHeight; } private void DataGrid_QueryRowHeight(object sender, Syncfusion.SfDataGrid.XForms.QueryRowHeightEventArgs e) { if(e.RowIndex > 0) { e.Height = SfDataGridHelpers.GetRowHeight(dataGrid, e.RowIndex); e.Handled = true; } }
To skip the header row the condition (e.RowIndex > 0) is added.
Screenshot
Sample Link
How to adjust row heights of each row based on their content?
Conclusion
I hope you enjoyed learning about how to adjust row heights of each row based on their content.
You can refer to our Xamarin.Forms DataGrid feature tour page to know about its other groundbreaking feature representations. You can also explore our Xamarin.Forms DataGrid documentation 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!