如何比较两个数据表行并创建一个新的数据表?

时间:2014-01-03 01:38:23

标签: asp.net vb.net gridview datatable dataset

我有两个数据表。一个具有帐户和余额的数据表,另一个数据表具有费用和余额。例如: 数据表1 111111 35.00 111112 50.00 数据表2 111111sf 5.00 111112sf 8.00

新数据表应显示数据表1中的帐户和总余额(数据表1和数据表2) 111111 40.00 111112 58.00 如何比较两个数据表以遍历每一行并添加这些值然后创建一个新的数据表?我用三个gridviews和List(Of string)做了它,但它很脏。我将不胜感激任何帮助。 这是我的代码:

    sqladapter = New SqlDataAdapter("SELECt account, balance FROM DEBT WHERE (account =   @account)", conn)
    sqladapter = New SqlDataAdapter("SELECt account, balance FROM DEBT WHERE (account = @account)", conn)
    Dim ds As New DataSet()


    sqladapter.SelectCommand.Parameters.AddWithValue("@account", 111111)
    sqladapter2.SelectCommand.Parameters.AddWithValue("@account", 111111)

    sqladapter.Fill(ds, "WithSFDT")
    sqladapter2.Fill(ds, "WithoutSFDT")


    'get datatable
    Dim dtWith As DataTable = ds.Tables("WithSFDT")
    Dim dtWithout As DataTable = ds.Tables("WithoutSFDT")

以下是gridview中的脏代码:

     Dim itemlist As New List(Of String)


        For Each row1 As GridViewRow In GridView1.Rows
            If row1.RowType = DataControlRowType.DataRow Then
                Dim gv1Value As String = row1.Cells(1).Text
                Dim gv1col3 As Decimal = row1.Cells(2).Text
                Dim lname As String = row1.Cells(3).Text
                Dim fname As String = row1.Cells(4).Text
                Dim TOdate As DateTime = row1.Cells(5).Text
                Dim debtnum As Integer = row1.Cells(6).Text



                For Each row2 As GridViewRow In GridView2.Rows
                    If row2.RowType = DataControlRowType.DataRow Then
                        Dim gv2Value As String = row2.Cells(3).Text
                        Dim gv2col3 As Decimal = row2.Cells(1).Text

                        If gv1Value.Contains(gv2Value) Then
                            GridView1.Visible = False
                            GridView2.Visible = False
                            GridView3.Visible = True


                            itemlist.Add(TOdate & " | " & gv1Value & " | " & fname & " " & lname & " " & " | " & gv1col3 + gv2col3 & " " & " | " & debtnum)

                        ElseIf gv2Value Is Nothing Then

                            GridView3.Visible = False
                            GridView1.Visible = True

                            'itemlist.Add(gv1col3 + gv2col3)
                            'list2.Add(gv1Value)

                        End If

                    End If
                Next

            End If


        Next
        If itemlist.Count > 0 Then


            GridView3.DataSource = itemlist
            GridView3.DataBind()
            GridView3.HeaderRow.Cells(0).Text = "T/O Date" & " |   " & " Account" & "   | " & " Name" & "   | " & "Balance" & " | " & "Debt #"
            ' GridView3.HeaderRow.Cells(1).Text = " Balance"
            'GridView4.Columns.Add(
            GridView3.Visible = True


        End If

问题在于Gridview3在一行中显示项目列表,它看起来不像一个表。

0 个答案:

没有答案