Frank
Frank

Reputation: 2045

Looping through records from one DataTable and populating another DataTable in vb.net

I have an asp.net application where I have a datatable ("DataTableA") that returns just one column ("ProductID").

I want to read each row of "ProductID", process some business logic, then copy both those columns (ProductID & ProductIDBusinessLogicValue) to DataTableB. This DataTableB is then displayed on the asp.net page.

What would bhe the best way to read each row of DataTableA ?

Thanks

Upvotes: 1

Views: 1340

Answers (2)

Chains
Chains

Reputation: 13167

Here's another approach you might like (linq)

Dim table1 As DataTable = ds.Tables("DataTableA")    
Dim query = From product In table1.AsEnumerable() Select ProductID, myCalculatedField    
Dim table2 As DataTable = query.CopyToDataTable()

More Info...

Upvotes: 1

Antonio Bakula
Antonio Bakula

Reputation: 20693

you can copy DataTableA to DataTableB, add column and do business logic on each row, something like this:

  DataTable dataTableB = dataTableA.Copy();
  dataTableB.Columns.Add("ProcessedValue", typeof(string));

  foreach (DataRow rw in dataTableB.Rows)
  {
    rw.SetField<string>("ProcessedValue", BusinessLogic(rw.Field<int>("ProductID")));
  }

Upvotes: 2

Related Questions