By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,424 Members | 1,351 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,424 IT Pros & Developers. It's quick & easy.

How do I get the row interation of a foreach statement?

P: n/a
I have this:

foreach (DataRow row in myDS.Tables["myTable"].Rows)
{
int ctr=0; //counter for child node

//create a parent node for each new row
trvRetrieved.Nodes.Add(new
TreeNode(myIdNumber));

//children of that parent row
trvRetrieved.Nodes[ctr].Nodes.Add(new
TreeNode(myPerson));
trvRetrieved.Nodes[ctr].Nodes.Add(new
TreeNode(myAddress));

ctr++;

}

Is there a way to get the current row iteration number I am on instead
of using the by hand int counter?

Thank you.
Jul 14 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Jul 14, 3:28*pm, jmDesktop <needin4mat...@gmail.comwrote:
I have this:

*foreach (DataRow row in myDS.Tables["myTable"].Rows)
{
* * * * * * * * * * * * int ctr=0; //counter for child node

* * * * * * * * * * * * //create a parent node for each new row
* * * * * * * * * * * * trvRetrieved.Nodes.Add(new
TreeNode(myIdNumber));

* * * * * * * * * * * * //children of that parentrow
* * * * * * * * * * * * trvRetrieved.Nodes[ctr].Nodes.Add(new
TreeNode(myPerson));
* * * * * * * * * * * * trvRetrieved.Nodes[ctr].Nodes.Add(new
TreeNode(myAddress));

* * * * * * * * * * * * ctr++;

}

Is there a way to get the current row iteration number I am on instead
of using the by hand int counter?
I have a "SmartEnumerable" which wraps any other IEnumerable<Tto
provide the index, along with whether the entry is first and/or last.
See http://www.yoda.arachsys.com/csharp/...numerable.html
for details.
It only works with generic IEnumerables, which may be an issue (IIRC,
the Rows property is nongeneric) but you could always duplicate the
code for a nongeneric version.

Jon
Jul 14 '08 #2

P: n/a
On Jul 14, 6:28*pm, jmDesktop <needin4mat...@gmail.comwrote:
I have this:

*foreach (DataRow row in myDS.Tables["myTable"].Rows)
{
* * * * * * * * * * * * int ctr=0; //counter for child node

* * * * * * * * * * * * //create a parent node for each new row
* * * * * * * * * * * * trvRetrieved.Nodes.Add(new
TreeNode(myIdNumber));

* * * * * * * * * * * * //children of that parentrow
* * * * * * * * * * * * trvRetrieved.Nodes[ctr].Nodes.Add(new
TreeNode(myPerson));
* * * * * * * * * * * * trvRetrieved.Nodes[ctr].Nodes.Add(new
TreeNode(myAddress));

* * * * * * * * * * * * ctr++;

}

Is there a way to get the current row iteration number I am on instead
of using the by hand int counter?
LINQ'ish solution:

foreach (var t in dataTable.Select((row, index) =new { Row = row,
Index = index })
{
// Use t.Row and t.Index as needed.
...
}

If necessary, you could speed that up a bit by using
KeyValuePair<DataRow, intrather than anonymous type (the former is a
value type, the latter is a reference type).
Jul 14 '08 #3

P: n/a
jmDesktop wrote:
I have this:

foreach (DataRow row in myDS.Tables["myTable"].Rows)
{
int ctr=0; //counter for child node

//create a parent node for each new row
trvRetrieved.Nodes.Add(new
TreeNode(myIdNumber));

//children of that parent row
trvRetrieved.Nodes[ctr].Nodes.Add(new
TreeNode(myPerson));
trvRetrieved.Nodes[ctr].Nodes.Add(new
TreeNode(myAddress));

ctr++;

}

Is there a way to get the current row iteration number I am on instead
of using the by hand int counter?

Thank you.
No, the enumerator doesn't expose the index of the current item.

You can rewrite the loop to use an index instead of an enumerator:

DataRowCollection rows = myDS.Tables["myTable"].Rows;
for (int ctr = 0; ctr < rows.Count; ctr++) {
DataRow row = rows[ctr];

//create a parent node for each new row
trvRetrieved.Nodes.Add(new TreeNode(myIdNumber));

//children of that parent row
trvRetrieved.Nodes[ctr].Nodes.Add(new TreeNode(myPerson));
trvRetrieved.Nodes[ctr].Nodes.Add(new TreeNode(myAddress));

}

--
Göran Andersson
_____
http://www.guffa.com
Jul 14 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.