【问题标题】:How to pull values from one gridview and insert them into another gridview asp vb如何从一个gridview中提取值并将它们插入另一个gridview asp vb
【发布时间】:2016-07-19 17:12:14
【问题描述】:

我目前有两个网格视图,一个来自 SQL 连接,另一个来自 OLEDB2 连接。我可以遍历这些网格视图并检查一个是否包含另一个不包含的值。

我的问题是,当我找到这些值时,我可以将它们放入最终的网格视图中以显示其他两个网格视图之间的共同值吗?

提前感谢您的帮助, 这就是我目前所拥有的。

    Puclic Sub CompareDB()
    Dim missingRecords As New DataTable
    missingRecords = GridView1.DataSource.clone()
    GridView3.DataSource = missingRecords
    GridView3.DataBind()

    Dim V1 As String = ""
    Dim V2 As String = ""
    Dim msg As String = ""
    Dim check As Boolean = False
    For Each row As GridViewRow In GridView2.Rows
        check = False
        For Each rw As GridViewRow In GridView1.Rows

            V1 = row.Cells(0).Text
            V2 = rw.Cells(0).Text

            V2 = Replace(V2, " ", "")
            If V1 = V2 Then
                check = True
                ' if check is true 
                ' insert the value V1 and V2 into GridView 3

                Exit For
            End If
        Next
        If check = False Then
            msg = msg & V1 & " -999 "
        End If
    Next
    msg = msg & "------------------------------------------------------------------ -999 "
    For Each row As GridViewRow In GridView1.Rows
        check = False
        For Each rw As GridViewRow In GridView2.Rows

            V1 = row.Cells(0).Text
            V2 = rw.Cells(0).Text

            V1 = Replace(V1, " ", "")
            If V1 = V2 Then
                check = True
                Exit For
            End If
        Next
        If check = False Then
            msg = msg & V1 & " -999 "
        End If
    Next
    ' after all checks complete, inserts the the non common values
    ' into gridview3
    ' EXAMPLE: GridView3
    ' gridview1 | gridview2
    ' v1        | V2
    ' v1        | V2
    ' non common|
    '           | non common

    msg = Replace(msg, "-999", "<br />")



    ' used to output for testing
    Label1.Text = msg
End Sub`

【问题讨论】:

  • 您可以使用 LINQ 比较这 2 个数据集创建一个新数据集以使用新数据集填充新的 gridview。

标签: asp.net vb.net gridview


【解决方案1】:

创建一个数据表并克隆其中一个网格视图源。然后根据需要将记录添加到数据表中,并设置一个新的网格视图,数据源为该表。有点像..

Dim GridView3 as New GridView 
Dim missingRecords as New Datatable
missingRecords = GridView1.dataSource.Clone()
'add your records here
GridView3.dataSource = missingRecords
GridView3.dataBind()

【讨论】:

  • 感谢卢克的快速回复。我尝试了您的建议,但我使用的是 gridviews 而不是 datagridviews。我已经在 aspx 页面中声明了 GridView3,但是在我执行 gridview3.datasource = missingRecords 之后,gridview 不显示任何内容。
  • 对不起,我相信你也需要 GridView3.dataBind()
  • 我在运行代码之前把它放在那里,但仍然一无所获。我编辑了我的帖子以显示代码,以便更好地显示我在做什么
  • 我会尝试在“If V1 = V2”if 语句中设置一个断点,以确保某些记录符合标准。如果这不是问题,请确保在 if 语句中将行添加到数据表中,并且在添加所有记录后将数据表设置为 gridView 的数据源。
  • 两列中的值相同。我只需要将它们一起输出到 GridView3 的两个不同列中,然后如果没有相似的值,它们将位于相应列中,而 GridView3 的相反列中为空白。我试着移动一些东西,它运行了我从你那里添加的代码,但它仍然没有显示 GridView3。
【解决方案2】:

我解决了。在下面发布我的问题的解决方案。

 Public Sub compareDBs()

    Dim dt As New DataTable

    dt.Columns.Add("FromPARC", Type.GetType("System.String"))
    dt.Columns.Add("FromJDE", Type.GetType("System.String"))
    Dim myrow As DataRow

    Dim V1 As String = ""
    Dim V2 As String = ""
    Dim msg As String = ""
    Dim check As Boolean = False

    For Each row As GridViewRow In GridView2.Rows
        check = False
        For Each rw As GridViewRow In GridView1.Rows
            V1 = row.Cells(0).Text
            V2 = rw.Cells(0).Text
            V2 = Replace(V2, " ", "")
            If V1 = V2 Then
                check = True
                myrow = dt.NewRow
                myrow("FromPARC") = V1
                myrow("FromJDE") = V2
                dt.Rows.Add(myrow)
                Exit For
            End If
        Next
        If check = False Then
            myrow = dt.NewRow
            myrow("FromPARC") = V1
            dt.Rows.Add(myrow)
            msg = msg & V1 & " -999 "
        End If
    Next

    msg = msg & "------------------------------------------------------------------ -999 "
    For Each row As GridViewRow In GridView1.Rows
        check = False
        For Each rw As GridViewRow In GridView2.Rows
            V1 = row.Cells(0).Text
            V2 = rw.Cells(0).Text
            V1 = Replace(V1, " ", "")
            If V1 = V2 Then
                check = True
                Exit For
            End If
        Next
        If check = False Then
            myrow = dt.NewRow
            myrow("FromJDE") = V1
            dt.Rows.Add(myrow)
            msg = msg & V1 & " -999 "
        End If
    Next

    GridView3.DataSource = dt
    GridView3.DataBind()

    msg = Replace(msg, "-999", "<br />")

    ' used to output for testing
    Label1.Text = msg
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多