Articles in this section
Category / Section

How to hightlight row when hovered in .NET MAUI DataGrid?

2 mins read

We can utilize the CellEntered and CellExited events of the .NET MAUI DataGrid to highlight the row when hovered.

C#

In the CellEntered event, pass the row index of the current row where the pointer has entered to set the background color of the row.

private void dataGrid_CellEntered(object sender, DataGridCellEnteredEventArgs e)
{
    var rowIndex = e.RowColumnIndex.RowIndex;
    var dataRow = GetDataRowBase(rowIndex);
    if (dataRow != null && rowIndex > 0)
    {
        GetDataGridRow(dataRow)!.BackgroundColor = Color.FromArgb("#caf0f8");
    }
}

In the CellExited event, we will pass the row index of the current row where the pointer has exited to revert the background color of the row.


private void dataGrid_CellExited(object sender, DataGridCellExitedEventArgs e)
{
    var rowIndex = e.RowColumnIndex.RowIndex;
    var dataRow = GetDataRowBase(rowIndex);
    if (dataRow != null && rowIndex > 0)
    {
        GetDataGridRow(dataRow)!.BackgroundColor = (this.dataGrid.DefaultStyle.RowBackground as SolidColorBrush)!.Color;
    }
}

We can use this helper method to get the row element.

private DataRowBase? GetDataRowBase(int index)
{
    return dataGrid.GetRowGenerator().Items!.FirstOrDefault(x => x.RowIndex == index);
}

private DataGridRow? GetDataGridRow(DataRowBase dataRowBase)
{
    var wholeRowElement = dataRowBase?.GetType()!.GetRuntimeFields().FirstOrDefault(x => x.Name.Equals("WholeRowElement"))!.GetValue(dataRowBase);
    return wholeRowElement as DataGridRow;

}

The following GIF demonstrates the highlight of the row when hovered.

SfDataGrid_Row_Hightlight.gif

Download the complete sample from GitHub

Conclusion
I hope you enjoyed learning how to highlight a row when hovering in the .NET MAUI DataGrid.

You can refer to our .NET MAUI DataGrid feature tour page to learn about its other groundbreaking feature representations. Explore our .NET MAUI DataGrid Documentation 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, support portal, 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