开发者

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

开发者 https://www.devze.com 2023-03-27 17:46 出处:网络
I have an asp.net application where I have a datatable (\"DataTableA\") that returns just one column (\"Product开发者_Python百科ID\").

I have an asp.net application where I have a datatable ("DataTableA") that returns just one column ("Product开发者_Python百科ID").

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


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")));
  }


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...

0

精彩评论

暂无评论...
验证码 换一张
取 消