PC Review Forums Newsgroups Microsoft DotNet Microsoft ADO .NET Row number in DataRow.

Reply

Row number in DataRow.

 
Thread Tools Rate Thread
Old 18-09-2006, 07:36 AM   #1
=?Utf-8?B?cHJhZGVlcF9UUA==?=
Guest
 
Posts: n/a
Default Row number in DataRow.


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
  Reply With Quote
Old 18-09-2006, 01:58 PM   #2
Marina Levit [MVP]
Guest
 
Posts: n/a
Default Re: Row number in DataRow.

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
newsE3F772F-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



  Reply With Quote
Old 18-09-2006, 05:13 PM   #3
=?Utf-8?B?cHJhZGVlcF9UUA==?=
Guest
 
Posts: n/a
Default Re: Row number in DataRow.

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
> newsE3F772F-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

>
>
>

  Reply With Quote
Old 18-09-2006, 05:21 PM   #4
Marina Levit [MVP]
Guest
 
Posts: n/a
Default Re: Row number in DataRow.

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
>> newsE3F772F-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

>>
>>
>>



  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off