如何从一个gridview中提取值并将它们插入到另一个gridview asp vb中

时间:2016-07-19 17:12:15

标签: asp.net vb.net gridview

我目前有两个网格视图,一个来自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`

2 个答案:

答案 0 :(得分:0)

创建数据表并克隆其中一个网格视图源。然后根据需要将记录添加到数据表中,并使用数据源设置新的网格视图作为此表。像...这样的东西。

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

答案 1 :(得分:0)

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

 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