473,385 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
3 3200
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Adam | last post by:
Hi, I am having a rather frustrating time with a script I am working on. I am looping through the elements of an array with a foreach statment and inside the foreach statement I have two if...
7
by: Phil | last post by:
Hi, I read somewhere that the new version (v1.1) has improved the performance of 'foreach' over 'for'. Is that true? I did some measurements and I still think for has an upperhand... ? Phil
5
by: Brad Williams | last post by:
I'm trying to get clearer on limitations of assignment/modifications within a foreach. Why does the following gives a compilation error if MyType is a struct, but it does not if MyType is a class?...
104
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars...
9
by: Anthony Bouch | last post by:
Everything I know about looping structures says to be careful about expressions that need to be evaluated again and again for each test/increment of a loop. I came across this piece of code the...
4
by: Sjoerd | last post by:
Summary: Use foreach(..) instead of while(list(..)=each(..)). --==-- Foreach is a language construct, meant for looping through arrays. There are two syntaxes; the second is a minor but useful...
5
by: ano | last post by:
I received this error when I used "ref" in foreach statement. Dictionary<string, object> oInterfaces; foreach (clsInterface vinterface in oInterfaces.Values) { func1( ref vinterface ); } ...
9
by: news.microsoft.com | last post by:
I am looping through an iteration and I would like to test the next item but if its not the one that I want how do I put it back so that when my foreach continues it is in the next iteration? ...
9
by: Nathan Sokalski | last post by:
I am trying to use the System.Array.ForEach method in VB.NET. The action that I want to perform on each of the Array values is: Private Function AddQuotes(ByVal value As String) As String Return...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.