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 hightlight the row when hovered.

C#

In the CellEntered event, we will 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 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 row when hovered.

SfDataGrid_Row_Hightlight.gif

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 highlight of row when hovered in .NET MAUI DataGrid?

You can refer to our .NET MAUI DataGrid’s feature tour page to know about its other groundbreaking feature representations. You can also explore our .NET MAUI DataGrid Documentation 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!

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