Question DataView Row Color

GoodJuJu

Member
Joined
Apr 26, 2010
Messages
24
Location
England
Programming Experience
5-10
Hi there,

I wonder if somebody could please help me. I am looping through a column in a DataView.

What I would like to do is to change the default BackColor of the row if my criteria is met (i.e. the current cell contains a particular string).

I can loop through the DataView without any problems, but I cannot seem to access, or modify the BackColor.

VB.NET:
Dim dv As New DataView(MyDbase.Tables(0))

            dv.Sort = "COL_0"

            For rc As Integer = 0 To dv.Table.Rows.Count - 1

                strThisCell = dv.Table.Rows(rc)("COL_0").ToString()

                If strMyString.ToLower().Contains(strThisCell.ToLower) Then

                    '  Change Row Color....
                    '  I cannot seem to get any of these to work

                    '  dv.Table.Rows(rc)("COL_0").DefaultCellStyle.BackColor = Color.Pink
                    '  dv.Table.Rows(rc)("COL_0").Items.BackColor = Color.Khaki
                    '  dv.Table.Rows(rc).Items.BackColor = Color.Khaki

                End If
                
            Next rc

I have searched the web quite a bit. Most of the results I have found talk about DataGridViews, not DataViews. Is there a big difference between the two? Do I need to switch to a DataGridView to be able to access the BackColor property?

Any help or guidance anyone can offer is much appreciated.
 
I do this in my app.

I use "traffic light" colours to highlight rows

This is my code:
VB.NET:
Public Sub ColourMyGrid()
        Dim varStatus As Integer
        For Each row As DataGridViewRow In Me.grdResults.Rows
            varStatus = row.Cells.Item("Status").Value
            If varStatus = "1" Then
                row.DefaultCellStyle.BackColor = Color.LightGreen
            ElseIf varStatus = "2" Then
                row.DefaultCellStyle.BackColor = Color.PaleGoldenrod
            ElseIf varStatus = "3" Then
                row.DefaultCellStyle.BackColor = Color.IndianRed
            ElseIf varStatus = "4" Then
                row.DefaultCellStyle.BackColor = Color.CadetBlue
            End If
        Next
    End Sub

I have a field called Status. This is simple an integer of 1,2,3 or 4 (1=good, 2=action needed, 3=bad, 4=authorise)
In that code varStatus = my status column value.
It then uses FOR...NEXT to loop through the rows, and IF...ELSEIF to set the row color.

Hope you can use that to achieve what you require
 
Last edited:
Hi Arg81,

Thanks for your reply. I wonder if you could help me a little bit more?

if I wanted to only search through the column "TAG_NUMBER", do you know how I could apply this to your code example?
 
is Tag Number string?

If so you'll use:

VB.NET:
Public Sub ColourMyGrid()
        Dim varTN As String
        For Each row As DataGridViewRow In Me.grdResults.Rows
            varTN = row.Cells.Item("TAG_NUMBER").Value
            If varTN = "ABC" Then
                row.DefaultCellStyle.BackColor = Color.LightGreen
            ElseIf varTN = "XYZ" Then
                row.DefaultCellStyle.BackColor = Color.PaleGoldenrod
            End If
        Next
    End Sub

varTN is declared as a string. If varTN = ABC then it changes row colour to light green, if varTN = XYZ it changes row colour to golden rod.

EDIT: I forgot to change STATUS to TAG_NUMBER when I first posted this. Changed now.
 
Back
Top