Articles in this section
Category / Section

How to change the row height event in .NET MAUI DataGrid?

3 mins read

The .NET MAUI DataGrid provides support for setting row height based on the row content using the QueryRowHeight event.

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 the 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);
        }
    }
}

Output

SfDataGrid_Dynamic_RowHeight.gif

Download the complete sample from GitHub

Conclusion

I hope you enjoyed learning how to change the row height event dynamically in .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 to understand how to present and manipulate data.

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

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