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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to compare rows in gridview 1

Status
Not open for further replies.

arada1

MIS
Dec 3, 2006
47
US
Hi all,
I am trying to compare rows and if there is a match between rows then I want to highlight it with a different color. I am working on the below code and so far it just convert the entire row to blue..any help would be appreciated.

eg. if row1=row2 then use blue color
else
use green....something like this


Protected Sub gvLongLat_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvLongLat.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim project As String = CType(DataBinder.Eval(e.Row.DataItem, "sp"), String)
If project = project Then
e.Row.BackColor = Drawing.Color.LightSkyBlue
End If
End If
End Sub
 
What exactly are you trying to compare and do?
The rowdatabound event fires as each row binds. So in your code you are comparing the row to itself and each row will be colored blue.
 
I have a column name project and sometimes the same project number is being used more than once and if this is the case then I want to change the color of rows to blue every time i have same project number. thanks for the help
 
Still not understanding what you are trying to do. What if you have multilple project numbers that are used more than once? Then you will have a bunch of blue rows. It won't really point anyting out to the user.
You should probably order your rows in your SQL by project number. Then in the row databound event, once the project number changes, change the color. This way each group of ids has it's own color.
 
Thank your reply. jbenson001 I like your idea of in the row databound event, once the project number changes, change the color. How can I acomplish this? once again thank you for your help.
 
YOu can give this a shot:
Code:
    Inherits System.Web.UI.Page

    Dim prevID, currID As Integer
    Dim colorNames As New ArrayList
    Dim newColor As String = "White"


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        GetColors()
    End Sub

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then

            currID = e.Row.Cells(0).Text
            If prevID <> currID Then
                prevID = currID
                newColor = Me.colorNames(GetRandomColor)
                e.Row.BackColor = System.Drawing.Color.FromName(newColor)
                'e.Row.BackColor = System.Drawing.Color.FromName(Me.colorNames(GetRandomColor))
            Else
                e.Row.BackColor = System.Drawing.Color.FromName(newColor)
            End If

        End If 'e.Row.RowType
    End Sub

    Function GetRandomColor() As Integer
        Dim rand As New Random
        Dim randColorIndex As Integer = rand.Next(0, colorNames.Count)
        'Next(MinVal is inclusive, MaxVal is not)
        Return randColorIndex
    End Function

    Sub GetColors()
        ' Get an array of PropertyInfo objects
        '  from System.Drawing.Color.
        Dim typ As System.Type = GetType(System.Drawing.Color)
        Dim aPropInfo As System.Reflection. _
           PropertyInfo() = typ.GetProperties
        For Each pi As System.Reflection. _
           PropertyInfo In aPropInfo
            If pi.PropertyType.Name = "Color" _
               And pi.Name <> "Transparent" Then
                colorNames.Add(pi.Name)
            End If
        Next
        'Return colorNames
    End Sub
 
Thank you jbenson001 ....I am working with your provided code and I am getting this error msg. ON Line 95. thanks

Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error:


Line 93: If e.Row.RowType = DataControlRowType.DataRow Then
Line 94:
Line 95: currID = CType(e.Row.Cells(0).Text, Integer)
Line 96: If prevID <> currID Then
Line 97: prevID = currID

 
That is not the code i provided.. my code worked when i tested it.
 
quick correction to my code.
Put this delcaration at the page level:
Code:
   Dim rand As New Random
AND Remove it from the GetRandomColor() sub.


 
thank you again and I found out that cell(0) in my database in not an integer field. some of the field value looks like this 5309-00009 and the error message was conversion from string "5309-0009" to type integer is not valid.
 
It is a little bit furstrating..I thought I had it to work now I am getting the below error msg. on line 99

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Line 97: If prevID <> currID Then
Line 98: prevID = currID
Line 99: newColor = Me.colorNames(GetRandomColor)
Line 100: e.Row.BackColor = System.Drawing.Color.FromName(newColor)
Line 101: 'e.Row.BackColor = System.Drawing.Color.FromName(Me.colorNames(GetRandomColor
 
If you changed any code, then you may get errors. I tested it and it works fine. Did you wrap somehting in the Page_Load in If Not Page.IsPostBack ?
 
Wow jbenson001 you are so awsome..yes, that was the probleme I put GetColors() between in Not Page.IsPostBack and I took it out of there now it works like a charm.I wish I was just like you..hope one day i will get there..
Thank you agin
 
Thanks :) And glad to help!

GetColors() needs to be run each time the page loads to fill the array. Or you could change it to store in a session variable or cache. But I just did it that way as a quick example.

I am by no means an expert. Just keep plugging along and you will learn lots of new stuff. I learn new stuff everyday. And this forum is an excellent resource.

Good luck!

Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top