radGridView MouseOver

1 Answer 658 Views
GridView
Curtis
Top achievements
Rank 1
Iron
Iron
Veteran
Curtis asked on 08 Sep 2022, 06:29 PM

Hey all!

I have a radGridView where i need to capture the mouse-over so i can assign a "MouseOver" background color at runtime. 

Some of my rows are default and will use the default "MouseOver" color and some of my rows are "custom" with a custom background color applied using ViewRowFormatting and so i would like a Custom "MouseOver" color.  At this time even the DEFAULT mouse-over color...does NOT apply to rows that have their background handled in ViewRowFormatting (I do not know why this is.)

So that's it :)

How do i capture pre-mouse-over painting and apply a different color?  Or maybe this would happen at the theme level with a replacement theme element?  I dont know i'm just rambling at this point :(

If anyone has done this type of customization please let me know and thanks all!

Cheers!  Curtis.

 

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 12 Sep 2022, 10:06 AM
Hello, Curtis, 

The appropriate way to customize the grid cells via code is to handle the ViewCellFormatting/RowFormatting events: https://docs.telerik.com/devtools/winforms/controls/gridview/cells/formatting-cells 

Since the style coming from the theme may differ, it is better to apply the colors to the cell elements than to the row elements. 

A better option is to specify the mouse over color for the grid cell element in the theme. Once you customize the theme in Visual Style Builder to achieve the desired look, you can apply the custom theme in your application:
https://docs.telerik.com/devtools/winforms/knowledge-base/customize-a-theme 

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Curtis
Top achievements
Rank 1
Iron
Iron
Veteran
commented on 12 Sep 2022, 05:26 PM

Hello Dess :) 

That does help a lot but my challenge is that i need multiple "MouseOver" colors, each one based on data found in a cell and whether or not the cell has been selected.

There's my Light and Medium BG Colors if the row is NOT selected and a slightly darker "Light and Medium" BG colors if the row IS selected - so what i'm trying to learn is how to control the mouse color dynamically based on the row's cell values and whether or not the row has been selected.

Hope this clarifies.

Dess | Tech Support Engineer, Principal
Telerik team
commented on 13 Sep 2022, 09:27 AM

Hello, Curtis, 

Using the Visual Style Builder is the preferred way for customizing the grid rows. However, it is possible to change mouse hover and selection colors through handling the MouseMove and the RowFormatting events. Consider the code snippet below: 

        public RadForm1()
        {
            InitializeComponent();
            this.radGridView1.MouseMove += radGridView1_MouseMove;
            this.radGridView1.ViewRowFormatting += radGridView1_ViewRowFormatting; 
        }

        private void radGridView1_ViewRowFormatting(object sender, RowFormattingEventArgs e)
        {
            if (e.RowElement.IsCurrent) 
            { 
                e.RowElement.BackColor = Color.Blue; 
            }
            else 
            { 
                e.RowElement.BackColor = Color.White; 
            }
            e.RowElement.DrawFill = true; 
            e.RowElement.GradientStyle = GradientStyles.Solid; 
        }

        GridRowElement hoveredRow; 

        private void radGridView1_MouseMove(object sender, MouseEventArgs e)
        {
            GridRowElement row = null; 
            RadElement element = this.radGridView1.ElementTree.GetElementAtPoint(e.Location); 
            if (element is GridRowElement) 
            { 
                row = (GridRowElement)element; 
            }
            if (element is GridCellElement) 
            { 
                row = element.FindAncestor<GridRowElement>(); 
            }
            if (row != null) 
            { 
                if (hoveredRow != null) 
                { 
                    hoveredRow.DrawFill = true; 
                    if (hoveredRow.IsCurrent) 
                    { 
                        hoveredRow.BackColor = Color.Blue; 
                    }
                    else 
                    { 
                        hoveredRow.BackColor = Color.White; 
                    }
                    hoveredRow.GradientStyle = GradientStyles.Solid; 
                }
                hoveredRow = row; 
                if (hoveredRow != null) 
                { 
                    hoveredRow.DrawFill = true; 
                    hoveredRow.BackColor = Color.Red; 
                    hoveredRow.GradientStyle = GradientStyles.Solid; 
                }
            }
        }

The attached gif file illustrates the achieved result. Note that this is just a sample approach and it may not cover all possible cases. Feel free to modify and extend it according to your requirements.

Curtis
Top achievements
Rank 1
Iron
Iron
Veteran
commented on 13 Sep 2022, 06:23 PM

Thank you so much Dess!

I have been using the Visual Style Builder quite a lot recently and it's growing on me :)

I just couldn't figure out how to get the grid to assign a different mouse-over based on the data container in cells the mouse is currently over - but the above code looks amazing and using both i'm sure i'll get this resolved!

I hope you and yours have a great day.

Curtis

Dess | Tech Support Engineer, Principal
Telerik team
commented on 16 Sep 2022, 10:22 AM

Visual Style Builder is suitable for applying general style for the different element states for the cell elements. If you need to have different colors according to the cell value, it is necessary to do it programmatically. A sample approach was suggested in my previous reply. 
Tags
GridView
Asked by
Curtis
Top achievements
Rank 1
Iron
Iron
Veteran
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or