Skip to main content Skip to footer

How to retain cell background color when selecting cells in VSFlexGrid

When any cell formatting is applied on VsFlexGrid 8.0, then on selecting the row, the formatting is not shown. This example explains the code to retain or keep the cell’s backcolor when a row is selected in VsFlexGrid 8.0. In this, the row highlighting is done using DrawCell Event. We need few external APIs for the implementation:

Private Declare Function SetBkColor Lib "gdi32" (ByVal hDC As Long, ByVal crColor As Long) As Long  
Private Declare Function SetRect Lib "user32" (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long  
Private Declare Function ExtTextOut Lib "gdi32" Alias "ExtTextOutA" (ByVal hDC As Long, ByVal x As Long, ByVal y As Long, ByVal wOptions As Long, lpRect As RECT, ByVal lpString As String, ByVal nCount As Long, lpDx As Long) As Long  
Private Type RECT  
 Left As Long  
 Top As Long  
 Right As Long  
 Bottom As Long  
End Type  
Private Const ETO_CLIPPED = 4  
Private Const ETO_GRAYED = 1  
Private Const ETO_OPAQUE = 2

Now we need to bind VsFlexGrid and apply backcolor for the cells in the form load event. Also, we will set the Highlight property to flexHighlightNever.

Private Sub Form_Load()  
  With VSFlexGrid1  
 .AddItem vbNullString  
 .TextMatrix(1, 1) = "a"  
 .Cell(flexcpBackColor, 1, 1) = vbRed  
 .Cell(flexcpBackColor, 2, 1) = vbGreen  
 .Cell(flexcpForeColor, 3, 1) = vbBlue  
 .Select 1, 1, 1, .Cols - 1 'you can see,After select the first row, the cell(1,1) backcolor is not red, but user need see the red backcolor whether select it or not.  
 .ShowCell 1, 1  
 Dim r%, c%  

 For r = 1 To .Rows - 1  
 For c = 1 To .Cols - 1  
 .TextMatrix(r, c) = "r" & r & "c" & c  
 Next  
 Next  

 'Set the following properties to create a transparent selection  
 .OwnerDraw = flexODOver  
 .HighLight = flexHighlightNever ' Making sure that the Highlight of complete row is avoided  
 End With  
End Sub  

For the custom highlighting, we will handle the VsFlexGrid's DrawCell event to create a border for the selected row. This is done so that user can see cell selection along with the cell's backcolor.

Private Sub VSFlexGrid1_DrawCell(ByVal hDC As Long, ByVal Row As Long, ByVal Col As Long, ByVal Left As Long, ByVal Top As Long, ByVal Right As Long, ByVal Bottom As Long, Done As Boolean)  
<pre> ' we only want selected rows, scrollable cells  
 If Me.VSFlexGrid1.IsSelected(Row) = False Then Exit Sub  
 If Col < Me.VSFlexGrid1.FixedCols Then Exit Sub  

 ' draw a blue border around each cell  
 Dim clr As Long  
 clr = SetBkColor(hDC, vbBlue)  

 Dim rc As RECT  

 SetRect rc, Left, Top, Right, Bottom  
 rc.Bottom = rc.Top + 1  
 ExtTextOut hDC, 0, 0, ETO_OPAQUE, rc, 0, 0, 0  

 SetRect rc, Left, Top, Right, Bottom  
 rc.Top = Bottom - 1  
 ExtTextOut hDC, 0, 0, ETO_OPAQUE, rc, 0, 0, 0  

 SetRect rc, Left, Top, Right, Bottom  
 rc.Right = rc.Left + 1  
 ExtTextOut hDC, 0, 0, ETO_OPAQUE, rc, 0, 0, 0  

 SetRect rc, Left, Top, Right, Bottom  
 rc.Left = rc.Right - 1  
 ExtTextOut hDC, 0, 0, ETO_OPAQUE, rc, 0, 0, 0  

 SetBkColor hDC, clr  
End Sub  


Using the above code, the final output can be compared to the normal behavior on HighLighting the row.