looping

  • Thread starter Thread starter Darren Spooner
  • Start date Start date
D

Darren Spooner

i need to loop through a table in a dataset, how do i do that useing for
each statement?
 
Index the Rows collection using a For Next or a For Each

dim i as Integer, dr as DataRow
For i = 0 to myDs("MyTable").Rows.Count-1
dr = Ds("MyTable").Rows(i)
... do what you need to do
Next i



--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
Darren Spooner said:
i need to loop through a table in a dataset, how do i do that useing for
each statement?

foreach (DataRow row in table.Rows)
{
....
}
 
Darren:

Bill V already posted the For i loop, if you need for each



foreach(DataRow dro in myDataTable.Rows){
//DoSomething.
}

using foreach is a bit slower so if speed is your primary concern, then the
index based iterator is quicker.
 
Darren asked for the for/each statement:

'Assume untyped DataSet Ds exists

dim dr as DataRow
For Each dr in Ds.Tables("MyTable").Rows
' ... do what you need to do
'dr(0) points to the first column of the current row.
Next
 
Actually, I should also ask the question: Why are you looping? There are
good reasons to do so, but if you're executing queries on a row-by-row basis
or even changing the data (to be posted back to the database) in the
DataSet, these actions can often be executed by code running on the server.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
Back
Top