PC Review
Forums
Newsgroups
Microsoft DotNet
Microsoft ADO .NET
Row number in DataRow.
Forums
Newsgroups
Microsoft DotNet
Microsoft ADO .NET
Row number in DataRow.
![]() |
Row number in DataRow. |
|
|
Thread Tools | Rate Thread |
|
|
#1 |
|
Guest
Posts: n/a
|
Hi All,
I am iterating through the rows in a table through foreach statement. I am using DataRow object here to store the row from the DataRow collection. Within the loop I need to find out the current row number. I could have used an integer variable outside the loop and used it as counter. I wanted to know if there is any better way of doing it, perhaps using some properties of DataRow object itself. foreach (DataRow row in dataset.Tables[0].Rows) { //I want to find the current row number here; } Thanks pradeep |
|
|
|
#2 |
|
Guest
Posts: n/a
|
This is not possible. A DataRow does not know its index in the collection.
You would have to figure it out by looping through the rows, comparing each row to the current row to see if it's it. Obviously this is very slow. I recommend you use a for loop instead of a for each loop to avoid having to do this. "pradeep_TP" <pradeepTP@discussions.microsoft.com> wrote in message news E3F772F-D99E-4C4F-8EB2-05824235FF73@microsoft.com...> Hi All, > > I am iterating through the rows in a table through foreach statement. I am > using DataRow object here to store the row from the DataRow collection. > Within the loop I need to find out the current row number. I could have > used > an integer variable outside the loop and used it as counter. I wanted to > know > if there is any better way of doing it, perhaps using some properties of > DataRow object itself. > > foreach (DataRow row in dataset.Tables[0].Rows) > { > //I want to find the current row number here; > } > > Thanks > pradeep |
|
|
|
#3 |
|
Guest
Posts: n/a
|
Thank you Marina for your suggestion..
I have found the solution myself. Here is the code that I wrote int rowNum; foreach (DataRow row in dataset.Tables[0].Rows) { rowNum = dataset.Tables[0].Rows.IndexOf(row); } - pradeep "Marina Levit [MVP]" wrote: > This is not possible. A DataRow does not know its index in the collection. > You would have to figure it out by looping through the rows, comparing each > row to the current row to see if it's it. Obviously this is very slow. I > recommend you use a for loop instead of a for each loop to avoid having to > do this. > > "pradeep_TP" <pradeepTP@discussions.microsoft.com> wrote in message > news E3F772F-D99E-4C4F-8EB2-05824235FF73@microsoft.com...> > Hi All, > > > > I am iterating through the rows in a table through foreach statement. I am > > using DataRow object here to store the row from the DataRow collection. > > Within the loop I need to find out the current row number. I could have > > used > > an integer variable outside the loop and used it as counter. I wanted to > > know > > if there is any better way of doing it, perhaps using some properties of > > DataRow object itself. > > > > foreach (DataRow row in dataset.Tables[0].Rows) > > { > > //I want to find the current row number here; > > } > > > > Thanks > > pradeep > > > |
|
|
|
#4 |
|
Guest
Posts: n/a
|
This looks like a new method in 2.0, was not there in previous versions.
However, using a for loop will still be faster then asking the rows collection to figure out the row number for every row. "pradeep_TP" <pradeepTP@discussions.microsoft.com> wrote in message news:84B71F7D-29ED-48E3-90E9-CD6DD0D4E0B5@microsoft.com... > Thank you Marina for your suggestion.. > > I have found the solution myself. Here is the code that I wrote > > int rowNum; > foreach (DataRow row in dataset.Tables[0].Rows) > { > rowNum = dataset.Tables[0].Rows.IndexOf(row); > } > > - pradeep > > "Marina Levit [MVP]" wrote: > >> This is not possible. A DataRow does not know its index in the >> collection. >> You would have to figure it out by looping through the rows, comparing >> each >> row to the current row to see if it's it. Obviously this is very slow. >> I >> recommend you use a for loop instead of a for each loop to avoid having >> to >> do this. >> >> "pradeep_TP" <pradeepTP@discussions.microsoft.com> wrote in message >> news E3F772F-D99E-4C4F-8EB2-05824235FF73@microsoft.com...>> > Hi All, >> > >> > I am iterating through the rows in a table through foreach statement. I >> > am >> > using DataRow object here to store the row from the DataRow collection. >> > Within the loop I need to find out the current row number. I could have >> > used >> > an integer variable outside the loop and used it as counter. I wanted >> > to >> > know >> > if there is any better way of doing it, perhaps using some properties >> > of >> > DataRow object itself. >> > >> > foreach (DataRow row in dataset.Tables[0].Rows) >> > { >> > //I want to find the current row number here; >> > } >> > >> > Thanks >> > pradeep >> >> >> |
|
![]() |
|
| Thread Tools | |
| Rate This Thread | |
|
|

Main Page 

E3F772F-D99E-4C4F-8EB2-05824235FF73@microsoft.com...
