asp.net VB - Compare two gridviews and change cell colour.

Soldato
Joined
8 Mar 2005
Posts
3,850
Location
London, UK
asp.net VB - Compare two gridviews and change cell colour based on unique / duplicate values.

I'm trying to perform a basic compare between two identically formatted gridviews where column1, which contains a value, is either unique or a duplicate of column1 in gridview2.

Suffice to say; I'm scratching my head. the gridviews are populated based on a sqldatasource control from a dropdownlist. That aspect works fine and for the moment I'll park trying to do that dynamically with whatever value is in the dropdownlist via autopostback.

So, to help myself I'll use a button1_click as the trigger to compare whatever is currently presented in each gridview.
Code:
   Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim arrayList1 As ArrayList = New ArrayList()
        Dim arrayList2 As ArrayList = New ArrayList()
        Dim arrayList3 As ArrayList = New ArrayList()
        Dim a As Integer
        Dim b As Integer
        For a = 0 To GridView1.Rows.Count - 1 Step 1
            For b = 0 To GridView1.Columns.Count - 1 Step 1
                arrayList1.Add(GridView1.Rows(a).Cells(b).Text)
            Next
        Next

        Dim c As Integer
        Dim d As Integer
        For c = 0 To GridView2.Rows.Count - 1 Step 1
            For d = 0 To GridView2.Columns.Count - 1 Step 1
                arrayList2.Add(GridView2.Rows(c).Cells(d).Text)
            Next
        Next

        If Not arrayList2.Contains(arrayList1) Then
            MsgBox("True")

        Else
            MsgBox("False")
        End If

    End Sub
I know the above is nonsense but it's the best start-point I can find with my google fu. I'm just trying to fathom how I a) go about performing the compare and b) perform the cell colour change based on whether it matches or not.

Code:
   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
            Dim view As DataRowView = DirectCast(e.Row.DataItem, DataRowView)
            Select Case Convert.ToString(view("CompareColumn"))
                Case "Low"
                    e.Row.Cells(8).BackColor = System.Drawing.Color.PaleGreen
                    Exit Select
                Case "Medium"
                    e.Row.Cells(8).BackColor = System.Drawing.Color.LightYellow
                    Exit Select
                Case "High"
                    e.Row.Cells(8).BackColor = System.Drawing.Color.Pink
                    Exit Select
            End Select
        End If
End Sub
I've only previously managed formatting within a sub and so I am unclear how you reference changes in cell behaviour outside of one.

I know it's a shambles; any pointers would be most appreciated.

TIA, Paul!
 
You absolute delight!

I need to play some more but this is great. Next up get it up refresh on AutoPostBack from the dropdownlist.

thank you.

e: yes, understood. in actuality the gridview is populated from the same sqlquery but with slightly different where conditions for the compare. I know it's a very silly way to this.

Next up; remove the need for the button and have the compare code run on the dropdownlist autopostback!
 
Last edited:
Hmm I got this far but the execution complains of returning more than 1 value.
Code:
SELECT        REPLACE(LastConnectionUser, 'DOMAIN\', '') AS PrimUser, DNSName, DLU, DGN,
                             (SELECT        REPLACE(LastConnectionUser, 'DOMAIN\', '') AS Duplicate
                               FROM            view_Broker
                               WHERE        (CMDBuild = 1803) AND (DGN = @DGN)) AS Duplicate
FROM            view_Broker AS view_Broker_1
WHERE        (CMDBuild = 7) AND (DLU < 8) AND (DGN = @DGN)


Back to the other, less efficient method! (:

dropdownlist
Code:
  <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="DGN" DataValueField="DGN" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
code behind
Code:
Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DropDownList1.SelectedIndexChanged
        Dim arrayList1 As ArrayList = New ArrayList()
        Dim arrayList2 As ArrayList = New ArrayList()

        Dim c As Integer
        For c = 0 To GridView2.Rows.Count - 1 Step 1
            arrayList1.Add(GridView2.Rows(c).Cells(0).Text)
        Next


        Dim a As Integer
        For a = 0 To GridView1.Rows.Count - 1 Step 1
            If arrayList1.Contains(GridView1.Rows(a).Cells(0).Text) Then
                GridView1.Rows(a).BackColor = System.Drawing.Color.Pink
            Else
                GridView1.Rows(a).BackColor = System.Drawing.Color.LightGreen
            End If
        Next

        Dim d As Integer
        For d = 0 To GridView1.Rows.Count - 1 Step 1
            arrayList2.Add(GridView1.Rows(d).Cells(0).Text)
        Next


        Dim b As Integer
        For b = 0 To GridView2.Rows.Count - 1 Step 1
            If arrayList2.Contains(GridView2.Rows(b).Cells(0).Text) Then
                GridView2.Rows(b).BackColor = System.Drawing.Color.Pink
            Else
                GridView2.Rows(b).BackColor = System.Drawing.Color.LightGreen
            End If
        Next
    End Sub
but the code doesn't fire.
 
Back
Top Bottom