如何绑定两个或多个数据表并在一个gridview中显示为一个结果表

时间:2016-05-28 03:23:00

标签: datatable

我有一个表onetime.its列是: columns(masterno,requisitionno,dept,userid,mat_code,material_long_text,prq_no,step1_userid,step1_approvaldate,step2_userid,step2_approvaldate,step3_userid,step3_approvaldate)

但我有两个条件。我的查询如下:

查询1:

Dim dt1 As New DataTable()

Dim dataadapater1 As New SqlDataAdapter("Select * from copyonetime where step2_userid is null and userid is not null ", New SqlConnection(connectionString))
dataadapater1.Fill(dt1)
GridView1.DataSource = dt1
GridView1.DataBind()

QUERY2:

Dim dt As New DataTable()

Dim dataadapater As New SqlDataAdapter("Select * from copyonetime where step3_userid is null and step2_userid is not null ", New SqlConnection(connectionString))
dataadapater.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()

如何绑定这两个表并将一个gridview中的数据表显示为一个结果表。

1 个答案:

答案 0 :(得分:0)

创建一个新的datatable-DT3并从第一个数据表-DT1复制数据,并使用第二个数据表-DT2复制数据,然后将datatable-DT3绑定到gridview。

        Dim dt1 As New DataTable

        dt1.Columns.Add("Id", GetType(Integer))
        dt1.Columns.Add("FirstName", GetType(String))
        dt1.Columns.Add("LastName", GetType(String))

        dt1.Rows.Add(2, "Kevin", "Rodger")
        dt1.Rows.Add(3, "Donald", "Kapp")
        dt1.Rows.Add(4, "Betty", "Hanna")

        Dim dt2 As New DataTable

        dt2.Columns.Add("Id", GetType(Integer))
        dt2.Columns.Add("FirstName", GetType(String))
        dt2.Columns.Add("LastName", GetType(String))

        dt2.Rows.Add(5, "Mike", "McAlister")
        dt2.Rows.Add(6, "Jason", "Peixito")
        dt2.Rows.Add(7, "Michael", "Jhonson")
        dt2.Rows.Add(8, "Josef", "Horn")

        Dim dt3 As New DataTable
        dt3.Columns.Add("Id", GetType(Integer))
        dt3.Columns.Add("FirstName", GetType(String))
        dt3.Columns.Add("LastName", GetType(String))

        dt3 = dt1.Copy()
        dt3.Merge(dt2)

        GridView1.DataSource = dt3
        GridView1.DataBind()