How to change the row height event in .NET MAUI DataGrid?
The .NET MAUI DataGrid provides support for setting row height based on the row content using the QueryRowHeight event.
In order to trigger this event at runtime, like resizing the windows or resizing the column, We can use the InvalidateRowHeight
method to invoke the event at runtime.
XAML
<syncfusion:SfDataGrid x:Name="dataGrid"
AutoGenerateColumnsMode="None"
AllowResizingColumns="True"
ColumnResizing="dataGrid_ColumnResizing"
GridLinesVisibility="Both"
HeaderGridLinesVisibility="Both"
x:DataType="viewModel:EmployeeViewModel"
QueryRowHeight="dataGrid_QueryRowHeight"
PropertyChanged="dataGrid_PropertyChanged"
ItemsSource="{Binding EmployeeInformation}">
<syncfusion:SfDataGrid.Columns>
<syncfusion:DataGridTextColumn HeaderText="Name" MappingName="Name" />
<syncfusion:DataGridTextColumn HeaderText="Employee ID" MappingName="EmployeeID" />
<syncfusion:DataGridTextColumn HeaderText="Telephone" MappingName="Telephone"/>
<syncfusion:DataGridTextColumn HeaderText="About" MappingName="About"/>
</syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>
C#
This event will set the row height based on its content.
private void ordersDataGrid_QueryRowHeight(object sender, Syncfusion.Maui.DataGrid.DataGridQueryRowHeightEventArgs e)
{
if (e.RowIndex > 0)
{
e.Height = e.GetIntrinsicRowHeight(e.RowIndex);
e.Handled = true;
}
}
This event will be called when the column is resized. We will get the visible rows in the view and then we will set the row height dynamically when the column is resized.
private void dataGrid_ColumnResizing(object sender, Syncfusion.Maui.DataGrid.DataGridColumnResizingEventArgs e)
{
var VisibleRowCount = dataGrid.GetVisualContainer().ScrollRows.GetVisibleLines().Count;
for (int i = 0; i < VisibleRowCount; i++)
{
dataGrid.InvalidateRowHeight(i);
}
}
This event will be called when the window or datagrid is resized. We will get the visible rows in the view, and then we will set the row height dynamically .
private void ordersDataGrid_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName!.Equals("Width"))
{
var VisibleRowCount = dataGrid.GetVisualContainer().ScrollRows.GetVisibleLines().Count;
for (int i = 0; i < VisibleRowCount; i++)
{
dataGrid.InvalidateRowHeight(i);
}
}
}
View sample in GitHub
Take a moment to pursue this documentation, where you can find more 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 change the row height event dynamically in .NET MAUI DataGrid.
You can refer to our .NET MAUI DataGrid’s 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 to understand how to present and manipulate data.
For current customers, you can check out our .NET MAUI 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 .NET MAUI DataGrid and other .NET MAUI components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac or feedback portal. We are always happy to assist you!