c# - 如何比较两个gridview并忽略里面的数据顺序

标签 c# sql asp.net vb.net

我正在处理一个需要比较两个 gridview 的程序。

So, I have two gridview

The first Gridview have a data that I added manually,

  column 1 | column 2 | column 3 
      a    |     d    |     g
      b    |     e    |     h
      c    |     f    |     j

The second gridview data is retrieve from database. The example retrieving result is like this:

  column 1 | column 3 | column 2 
      a    |     g    |     d
      b    |     h    |     e
      c    |     j    |     f

Or could be like this:

  column 2 | column 1 | column 3 
      d    |     a    |     g
      e    |     b    |     h
      f    |     c    |     j

我使用这段代码从数据库中检索数据:

    Protected Sub btnCheck_Click(sender As Object, e As EventArgs) Handles btnCheck.Click
    sql = txtQuery.Text
    cmd = New SqlCommand(sql, con)
    drDataReader = cmd.ExecuteReader
    dtDatatable.Load(drDataReader)
    GridView1.DataSource = dtDatatable
    GridView1.DataBind()
    GridView1.Visible = True
End Sub

简而言之,第二个gridview的获取取决于用户在txtQuery.text中的输入,我想比较两个gridview并检查是TRUE还是FALSE。如上表一样,不考虑数据先后顺序,结果相同则显示true,错误则显示false。

已编辑:我尝试将 gridview 值存储到一个数组中并比较该数组。比较是检查arraylist2中的所有数据是否存在于arraylist1中,以及arraylist1中的数据是否存在于arraylist2中。我尝试使用此代码进行第一次比较,但它似乎不起作用。

Protected Sub btnCheck_Click(sender As Object, e As EventArgs) Handles btnCheck.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

我不知道为什么,但上面的代码总是显示“TRUE”。

代码有问题吗? 我认为它读取行号而不是每行的字符串。

提前致谢。

最佳答案

错误在:

arrayList2.Contains(arrayList1)

contains() 方法只查找单个值。不是为了完整的值(value) list 。

关于c# - 如何比较两个gridview并忽略里面的数据顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27146276/

相关文章:

sql - 使用 SQL Server 中的条件从存储过程中调用存储过程的正确语法是什么

sql - 从多行中获取一行

c# - 如何使存储为 C# 字符串的 HTML 元素正确显示?

c# - 客户端 ID 和 secret

asp.net - Application_End 全局.asax

c# - 如何正式定义这个 'var' 类型呢?

c# - 在XAML中获得子控件的更聪明的方法是什么?

c# - 并行运行异步方法 8 次

sql - 查询仅从字符串中获取数字

c# - NHibernate 存储过程 - 设置参数大小?