Articles in this section
Category / Section

How to apply the background color when mouse hover on SfDataGrid in .NET MAUI?

3 mins read

The .NET MAUI DataGrid allows you to change the background color of the DataGrid when hovering over a cell on WinUI and MacOS platforms.

    public class DataGridBehavior : Behavior<ContentPage>
    {
        private SfDataGrid? dataGrid;
        private EmployeeViewModel? employeeViewModel;

        protected override void OnAttachedTo(ContentPage page)
        {
            base.OnAttachedTo(page);
            employeeViewModel = new EmployeeViewModel();
            dataGrid = page.FindByName<SfDataGrid>("dataGrid");
            dataGrid.ItemsSource = employeeViewModel.Employees;
#if WINDOWS || MACCATALYST
            dataGrid.CellHovered += DataGrid_CellHovered;
            dataGrid.CellExited += DataGrid_CellExited;
#endif
        }

#if WINDOWS || MACCATALYST
        private void DataGrid_CellExited(object? sender, DataGridCellExitedEventArgs e)
        {
            if(this.dataGrid != null && this.dataGrid.BackgroundColor != Colors.Transparent)
            {
                this.dataGrid.BackgroundColor = Colors.Transparent;
            } 
        }

        private void DataGrid_CellHovered(object? sender, DataGridCellHoveredEventArgs e)
        {
            if (this.dataGrid != null && this.dataGrid.BackgroundColor == Colors.Transparent)
            {
                this.dataGrid.BackgroundColor = Colors.Red;
            }
        }
#endif

        protected override void OnDetachingFrom(ContentPage page)
        {
            base.OnDetachingFrom(page);

#if WINDOWS || MACCATALYST
            if (dataGrid != null)
            {
                dataGrid!.CellHovered -= DataGrid_CellHovered;
                dataGrid.CellExited -= DataGrid_CellExited;
            }
#endif
        }
    }

Output

MouseHover.PNG

Download the complete sample from GitHub

Conclusion

I hope you enjoyed learning how to apply background color when hovering over the SfDataGrid in .NET MAUI DataGrid (SfDataGrid).

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 on the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to explore 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, or the 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