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 ("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...
精彩评论