Category / Section
How to set the selection color for CurrentCell?
1 min read
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
Sample Link:
C#: Selection color for currentcell_CS
VB: Selection color for currentcell_VB