We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Changing Style on Cell hover

Thread ID:

Created:

Updated:

Platform:

Replies:

144887 May 27,2019 06:47 PM UTC May 28,2019 07:14 AM UTC WinForms 1
loading
Tags: SfDataGrid
Julius
Asked On May 27, 2019 06:47 PM UTC

I am trying to change the style of a row on cell hover. I was using the mouseHover feature but wasn't able to get the row it hovered and change that specific row style. I would like it that when it's hovered, the row would turn a different color.

You could do this easily with the datagridview with the cellcontentHover function, but didn't know how to implement this on the sfDataGrid since it's focused on the column.

Jagadeesan Pichaimuthu [Syncfusion]
Replied On May 28, 2019 07:14 AM UTC

Hi Julius, 
  
Thanks for using Syncfusion products. 
  
If you want to change the color of the hovered row, you can use the TableControl.MouseMove and QueryCellStyle events. Refer to the following snippet code, 
  
Code Snippet: 
int hoveredRowIndex = -1; 
  
sfDataGrid1.TableControl.MouseMove += TableControl_MouseMove; 
sfDataGrid1.QueryCellStyle += sfDataGrid1_QueryCellStyle; 
sfDataGrid1.TableControl.MouseLeave += TableControl_MouseLeave; 
  
void sfDataGrid1_QueryCellStyle(object sender, Syncfusion.WinForms.DataGrid.Events.QueryCellStyleEventArgs e) 
{ 
    if (e.RowIndex == hoveredRowIndex) 
    { 
        //Set the back color for the hovered row cells. 
        e.Style.BackColor = Color.Yellow; 
    } 
} 
  
void TableControl_MouseMove(object sender, MouseEventArgs e) 
{ 
    var rowColumnIndex =this.sfDataGrid1.TableControl.PointToCellRowColumnIndex(this.sfDataGrid1.TableControl.PointToClient(Cursor.Position)); 
  
    // Update the hovered row index. 
    if (hoveredRowIndex != rowColumnIndex.RowIndex) 
    { 
        sfDataGrid1.TableControl.Invalidate(sfDataGrid1.TableControl.GetRowRectangle(hoveredRowIndex,true)); 
        hoveredRowIndex = rowColumnIndex.RowIndex; 
        sfDataGrid1.TableControl.Invalidate(sfDataGrid1.TableControl.GetRowRectangle(hoveredRowIndex, true)); 
    }              
} 
  
void TableControl_MouseLeave(object sender, EventArgs e) 
{ 
    //To remove the hovered row color while the mouse is leaves the SfDataGrid. 
    sfDataGrid1.TableControl.Invalidate(sfDataGrid1.TableControl.GetRowRectangle(hoveredRowIndex, true)); 
    hoveredRowIndex = -1; 
} 
  
  
 
Let us know whether this helps also if you need any further assistance on this.  
 
Regards, 
Jagadeesan 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon