locked
2 gridview compare RRS feed

  • Question

  • User66371569 posted

    i am using this code  and working fine

     For Each row As GridViewRow In Me.GridView4.Rows
    
                For Each row2 As GridViewRow In Me.GridView5.Rows
                    Dim term = DirectCast(row.FindControl("term"), Label)
    
                    Dim term2 = DirectCast(row2.FindControl("term"), Label)
                    ' If term.Text = term2.Text Then
                    'row2.Cells(3).BackColor = Drawing.Color.LightGreen
                    ' End If
    
                    If term2.Text = term.Text Then
                        row2.Cells(3).BackColor = Drawing.Color.Green
                    End If
    
                    
    
    
               Next
            Next

    when i change

    If term2.Text <> term.Text Then

    to get mismatched     code not working 

    how to solve it 

    Wednesday, April 28, 2021 6:13 PM

Answers

  • User475983607 posted

    thepast

    I am wondring  why    <>  and   is not    not working    only   = is working

    i tried everything    still not working

    I created a demo and the <> operator works as expected.  Below is what demo code looks like.  If you are still having trouble then share enough code to reproduce your issue.  Use descriptive words to describe what is happening in the code.  "Not working" tells us nothing.

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="FormEx.aspx.vb" Inherits="WebFormsVBDemo.FormEx" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                    <Columns>
                        <asp:BoundField DataField="Id" HeaderText="Id" />
                        <asp:TemplateField HeaderText="Val">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Val") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Val") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Val2">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Val2") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("Val2") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>
        </form>
    </body>
    </html>
    
    Public Class Data
        Public Property Id As Integer
        Public Property Val As Decimal
        Public Property Val2 As Decimal
    End Class
    
    Public Class FormEx
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Page.IsPostBack Then
                GridView1.DataSource = PopulateData()
                GridView1.DataBind()
            End If
            FormatGridView()
        End Sub
    
        Protected Sub FormatGridView()
            For Each row As GridViewRow In GridView1.Rows
                Dim val As Decimal = Decimal.Parse(DirectCast(row.Cells(1).FindControl("Label1"), Label).Text)
                Dim val2 As Decimal = Decimal.Parse(DirectCast(row.Cells(1).FindControl("Label2"), Label).Text)
    
                If val <= 0 Then
                    row.Cells(1).BackColor = Drawing.Color.Red
                End If
                If val > 0 And val < 60 Then
                    row.Cells(1).BackColor = Drawing.Color.Orange
                End If
                If val >= 60 Then
                    row.Cells(1).BackColor = Drawing.Color.Yellow
                End If
    
                If val <> val2 Then
                    row.Cells(2).BackColor = Drawing.Color.Purple
                End If
            Next
        End Sub
    
        Protected Function PopulateData() As List(Of Data)
            Return New List(Of Data) From
                {
                    New Data With {.Id = 1, .Val = -1280, .Val2 = -1280},
                    New Data With {.Id = 2, .Val = 33, .Val2 = 33},
                    New Data With {.Id = 3, .Val = 5120, .Val2 = 5120},
                    New Data With {.Id = 4, .Val = 0, .Val2 = 1}
                }
        End Function
    
    End Class

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 29, 2021 11:08 AM

All replies

  • User475983607 posted

    Share code and data to reproduce this issue.  Explain the expected results and the actual results. 

    Wednesday, April 28, 2021 6:44 PM
  • User-1716253493 posted
    If Not term2.Text = term.Text Then

    To compare row by row

    For Each row As GridViewRow In Me.GridView4.Rows
          Dim row2 As GridViewRow = GridView5.Rows(row.RowIndex)

    Wednesday, April 28, 2021 8:47 PM
  • User1535942433 posted

    Hi thepast,

    As far as I think that <> should work fine.The same you could use If not.

    However,I suggest you could bebug and breakpoint at these lines to check what's wrong with it.

    Best regards,

    Yijing Sun

    Thursday, April 29, 2021 5:21 AM
  • User66371569 posted

    I am wondring  why    <>  and   is not    not working    only   = is working

    i tried everything    still not working

    Thursday, April 29, 2021 6:23 AM
  • User475983607 posted

    The not equal operator <> works exactly as written in the reference documentation.  Most likely, your expectation is incorrect or the data is not expected.   Share code that reproduces this issue and explain how you expect the code to work and what actually happens.  Keep in mind, we have no idea what "not working" means to you and we do not have the data. 

    Thursday, April 29, 2021 10:20 AM
  • User475983607 posted

    thepast

    I am wondring  why    <>  and   is not    not working    only   = is working

    i tried everything    still not working

    I created a demo and the <> operator works as expected.  Below is what demo code looks like.  If you are still having trouble then share enough code to reproduce your issue.  Use descriptive words to describe what is happening in the code.  "Not working" tells us nothing.

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="FormEx.aspx.vb" Inherits="WebFormsVBDemo.FormEx" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                    <Columns>
                        <asp:BoundField DataField="Id" HeaderText="Id" />
                        <asp:TemplateField HeaderText="Val">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Val") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Val") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Val2">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Val2") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("Val2") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>
        </form>
    </body>
    </html>
    
    Public Class Data
        Public Property Id As Integer
        Public Property Val As Decimal
        Public Property Val2 As Decimal
    End Class
    
    Public Class FormEx
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Page.IsPostBack Then
                GridView1.DataSource = PopulateData()
                GridView1.DataBind()
            End If
            FormatGridView()
        End Sub
    
        Protected Sub FormatGridView()
            For Each row As GridViewRow In GridView1.Rows
                Dim val As Decimal = Decimal.Parse(DirectCast(row.Cells(1).FindControl("Label1"), Label).Text)
                Dim val2 As Decimal = Decimal.Parse(DirectCast(row.Cells(1).FindControl("Label2"), Label).Text)
    
                If val <= 0 Then
                    row.Cells(1).BackColor = Drawing.Color.Red
                End If
                If val > 0 And val < 60 Then
                    row.Cells(1).BackColor = Drawing.Color.Orange
                End If
                If val >= 60 Then
                    row.Cells(1).BackColor = Drawing.Color.Yellow
                End If
    
                If val <> val2 Then
                    row.Cells(2).BackColor = Drawing.Color.Purple
                End If
            Next
        End Sub
    
        Protected Function PopulateData() As List(Of Data)
            Return New List(Of Data) From
                {
                    New Data With {.Id = 1, .Val = -1280, .Val2 = -1280},
                    New Data With {.Id = 2, .Val = 33, .Val2 = 33},
                    New Data With {.Id = 3, .Val = 5120, .Val2 = 5120},
                    New Data With {.Id = 4, .Val = 0, .Val2 = 1}
                }
        End Function
    
    End Class

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 29, 2021 11:08 AM