SaurabhSuman
SaurabhSuman

Reputation: 25

looking up duplicate columns from a datatable and add them to another datatable

I have a datatable with six rows and quite a number of columns. the columns are named as Operation1, folder1, quantity1, OPeration2, folder2, quantity2, and so on... The code is as follows:

    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)

What i now need to do is to find duplicate folder columns in any of the six rows and to sum up the corresponding quantity columns and store the folder name and the total quantity for retrieval later on... Is there any way of achieving this? Please help...

Upvotes: 0

Views: 678

Answers (1)

Sweety
Sweety

Reputation: 159

This is how to retrieve a Column Name from a DataColumn:

dt.Columns(1).ColumnName 

To get the name of all DataColumns within your DataTable:

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

You can perform search into name array to find matching column name and also add quantity.

Upvotes: 1

Related Questions