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: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to set the selection color for CurrentCell?

Platform: WinForms |
Control: GridControl |
Published Date: May 31, 2018 |
Last Revised Date: May 27, 2019

By default, the selection backcolor will not be drawn for current cell. In order to set the selection backcolor for current cell also, set backcolor for that cell in QueryCellInfo event and refresh that cell in SelectionChanged event.

Code Snippet

C#

//Event Subscription.
this.gridControl1.SelectionChanged += gridControl1_SelectionChanged;
this.gridControl1.QueryCellInfo += gridControl1_QueryCellInfo;
 
//Event Customization
 private void gridControl1_SelectionChanged(object sender, GridSelectionChangedEventArgs e)
 {
     selectedrange = e.Range;
     GridCurrentCell currentCell = this.gridControl1.CurrentCell;
     this.gridControl1.InvalidateRange(GridRangeInfo.Cell(currentCell.RowIndex, currentCell.ColIndex));
 }
 
private void gridControl1_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e)
 {
     GridRangeInfo range = GridRangeInfo.Cell(e.Style.CellIdentity.RowIndex, e.Style.CellIdentity.ColIndex);
     GridCurrentCell currentCell = gridControl1.CurrentCell;
     if (selectedrange.Contains(range) && !range.IsEmpty && currentCell != null
         && e.Style.CellIdentity.ColIndex == currentCell.ColIndex && e.Style.CellIdentity.RowIndex == currentCell.RowIndex)
     {
         e.Style.BackColor = this.gridControl1.Model.Options.AlphaBlendSelectionColor;
     }
 }

 

VB

'Event Subscription.
AddHandler Me.gridControl1.SelectionChanged, AddressOf gridControl1_SelectionChanged
AddHandler Me.gridControl1.QueryCellInfo, AddressOf gridControl1_QueryCellInfo
 
'Event Customization
Private Sub gridControl1_SelectionChanged(ByVal sender As Object, ByVal e As GridSelectionChangedEventArgs)
 selectedrange = e.Range
 Dim currentCell As GridCurrentCell = Me.gridControl1.CurrentCell
    Me.gridControl1.InvalidateRange(GridRangeInfo.Cell(currentCell.RowIndex, currentCell.ColIndex))
End Sub
 
Private Sub gridControl1_QueryCellInfo(ByVal sender As Object, ByVal e As GridQueryCellInfoEventArgs)
    Dim range As GridRangeInfo = GridRangeInfo.Cell(e.Style.CellIdentity.RowIndex, e.Style.CellIdentity.ColIndex)
    Dim currentCell As GridCurrentCell = gridControl1.CurrentCell
    If selectedrange.Contains(range) AndAlso (Not range.IsEmpty) AndAlso currentCell IsNot Nothing AndAlso e.Style.CellIdentity.ColIndex = currentCell.ColIndex AndAlso e.Style.CellIdentity.RowIndex = currentCell.RowIndex Then
        e.Style.BackColor = Me.gridControl1.Model.Options.AlphaBlendSelectionColor
    End If
End Sub

 

Screenshot

Showing current cell selection

 

Sample Link:

C#: Selection color for currentcell_CS

VB: Selection color for currentcell_VB

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

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

Up arrow icon

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