Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to change a datagrid's backcolor only for 1 row 1

Status
Not open for further replies.

RubenV

Programmer
Feb 17, 2005
34
BE
Hi there,

depending on the data in a datagrid's row, that row must have a specific backcolor.

How to do this?
By this code, I check the data, now I only need to set the backcolor for that row.

I hope someone can help, it's pretty urgent.

Many thanks already.
Cheers,
Ruben.
 
Sorry,

I forgot to post the code I use to check the data with.

On Error Resume Next
With dgRltdCompanies
For i = 0 To 100
.Row = i
If .Columns(2) = 0 Then
.BackColor = &H80000002
End If
Next i
End With

As you can see, I set the backcolor, but this way, each row and column's backcolor is set like this.

Regards,
Ruben.
 
here is a chunk of code to change the grid color one by one. hope it is useful!

Code:
Public Sub FG_AlternateRowColors(FG As MSFlexGrid, lColor1 As Long, lcolor2 As Long)
  Dim lRow As Long, lCol As Long
  Dim lOrgRow As Long, lOrgCol As Long
  Dim lColor As Long
  
  With FG ' MSFlexGrid1
    .Redraw = False
    
    ' save the current cell position
    lOrgRow = .Row
    lOrgCol = .col
    
    ' only the data rows
    For lRow = .FixedRows To .Rows - 1
      .Row = lRow
      
      If lRow / 2 = lRow \ 2 Then
        lColor = lColor1
      Else
        lColor = lcolor2
      End If
      
      ' only the data columns
      For lCol = .FixedCols To .Cols - 1
        .col = lCol
        .CellBackColor = lColor
      Next lCol
    Next lRow
    
    ' restore the orginal cell position
    .Row = lOrgRow
    .col = lOrgCol
    .Redraw = True
  End With
End Sub
 
Thx a lot for your reply !

is there no way to do this with a datagrid control in stead of a flexgrid?

Cheers Ruben.
 
I don't believe the datagrid has that capability. If you search the forum you may find a previous discussion on the subject and get a definetive answer.

Here is code that I use to change the backcolor of one row
Code:
   With MSFlexGrid1
      .FillStyle = flexFillRepeat 'repeat for entire range
      .Row = 3             'starting row
      .RowSel = 3          'ending row
      .Col = 0             'strating col
      .ColSel = .Cols - 1  'ending col
      .CellBackColor = vbcyan
   End With


zemp
 
thx a lot for your reply!

I went for the MSHFlexGrid and I think I got it working.
Just a small test left to do.

I'll keep you posted.

Thx again mate,
Ruben.
 
Woohoo,

it works. I really appreciate your help, both of you.

this is the code I used now
Code:
On Error Resume Next
        With mshfCompanies
        Dim strRelatedTo As String
            For k = 1 To dcRltdCompanies.Recordset.RecordCount
                .Row = k 'current row
                With mshfCompanies
                    .Col = 2
                    strRelatedTo = .Text
                End With
                If Not strRelatedTo <> "0" Then
                    .FillStyle = flexFillRepeat 'repeat for entire range
                    .RowSel = k          'ending row
                    .Col = 0             'starting col
                    .ColSel = .Cols - 1  'ending col
                    .CellBackColor = &H80110202
                End If
            Next k
        End With

Thx for letting me peek :)

Ruben
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top