从数据表中查找重复列并将它们添加到另一个数据表中

时间:2013-04-22 03:39:38

标签: vb.net datatable duplicates

我有一个包含六行和相当多列的数据表。列被命名为Operation1,folder1,quantity1,OPeration2,folder2,quantity2等等......代码如下:

    Dim dt As New DataTable

    'adding columns to the datatble
    For i = 1 To 60
        dt.Columns.Add("Operation" & i)
        dt.Columns.Add("folder" & i)
        dt.Columns.Add("quantity" & i)
    Next

    'adding datarows
    Dim dr1 As DataRow = dt.NewRow
    Dim dr2 As DataRow = dt.NewRow
    Dim dr3 As DataRow = dt.NewRow
    Dim dr4 As DataRow = dt.NewRow
    Dim dr5 As DataRow = dt.NewRow
    Dim dr6 As DataRow = dt.NewRow

    For i = 1 To 10
        dr1("Operation" & i) = GroupBox1.Controls("l1ob" & i)
        dr1("Folder" & i) = GroupBox1.Controls("l1f" & i)
        dr1("Quantity" & i) = GroupBox1.Controls("l1Qty" & i)
        dr2("Operation" & i) = GroupBox2.Controls("l2ob" & i)
        dr2("Folder" & i) = GroupBox2.Controls("l2f" & i)
        dr2("Quantity" & i) = GroupBox2.Controls("l2Qty" & i)
        dr3("Operation" & i) = GroupBox3.Controls("l3ob" & i)
        dr3("Folder" & i) = GroupBox3.Controls("l3f" & i)
        dr3("Quantity" & i) = GroupBox3.Controls("l3Qty" & i)
        dr4("Operation" & i) = GroupBox4.Controls("l5ob" & i)
        dr4("Folder" & i) = GroupBox4.Controls("l5f" & i)
        dr4("Quantity" & i) = GroupBox4.Controls("l5Qty" & i)
        dr5("Operation" & i) = GroupBox5.Controls("l6ob" & i)
        dr5("Folder" & i) = GroupBox5.Controls("l6f" & i)
        dr5("Quantity" & i) = GroupBox5.Controls("l6Qty" & i)
        dr6("Operation" & i) = GroupBox6.Controls("l4ob" & i)
        dr6("Folder" & i) = GroupBox6.Controls("l4f" & i)
        dr6("Quantity" & i) = GroupBox6.Controls("l4Qty" & i)
    Next

    'adding the rows to the datatble
    dt.Rows.Add(dr1)
    dt.Rows.Add(dr2)
    dt.Rows.Add(dr3)
    dt.Rows.Add(dr4)
    dt.Rows.Add(dr5)
    dt.Rows.Add(dr6)

我现在需要做的是在六行中的任何一行中找到重复的文件夹列并总结相应的数量列并存储文件夹名称和总数量以便以后检索...有没有办法实现这个?请帮忙......

1 个答案:

答案 0 :(得分:1)

这是从DataColumn中检索列名的方法:

dt.Columns(1).ColumnName 

获取DataTable中所有DataColumns的名称:

Dim name(dt.Columns.Count) As String
Dim i As Integer = 0
For Each column As DataColumn In DT.Columns
  name(i) = column.ColumnName
  i += 1
Next

您可以搜索名称数组以查找匹配的列名称,也可以添加数量。