DataRow Index

  • Thread starter Thread starter Mauricio
  • Start date Start date
M

Mauricio

How can I know the index of a DataRow that belongs to a
DataTable ?
I have the DataRow and I want your Rows' index.

Tanks.

Mauricio (Brazil)
 
The ordinal? The number of the row in the Rows collection? The Value of the
PK column?

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
MVP, hRD
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.
__________________________________
 
No not the The Value of the PK column
how do we get the ordinal, the number of the row in the
Rows collection.
 
The Ordinal property?

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
MVP, hRD
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.
__________________________________
 
Hi Mauricio,

Develop a dataview of the table and then you will get the index. Here's how
it works:

Dim arrayseeks(0) As Object

Dim ifinds As Integer

Dim vues As New DataView(dsshipcode.Tables(0))

vues.Sort = "shipcode"

Dim irow as datarow

For Each irow In dsshipcode.Tables(0).Rows

arrayseeks(0) = "12345"

ifinds = vues.Find(arrayseeks)

If ifinds <> -1 Then ' ie, found it

mdescrip = vues(ifinds)("descrip")

Else

mdescrip = ""

End If

Next

In this code, ifind in the index of the found row. I suspect this is what
you were after.
HTH,
Bernie Yaeger
 
Thanks for Bernie's response.

Hi Mauricio,

Assuming that you have both references to the DataTable and DataRow
objects, and the DataRow is in DataTable.Rows collection, you can also use
Ojbect.ReferenceEquals method to get the index. Here's an example:

private int FindIndex(DataTable dt, DataRow dr)
{
for(int i=0;i<dt.Rows.Count;i++)
if(Object.ReferenceEquals(dt.Rows, dr))
return i;
return -1;
}

For more information about Object.ReferenceEquals, please refer to the
following:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemobjectclassreferenceequalstopic.asp

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

--------------------
| From: "Bernie Yaeger" <[email protected]>
| References: <[email protected]>
| Subject: Re: DataRow Index
| Date: Sat, 8 Nov 2003 23:48:44 -0500
| Lines: 49
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.adonet
| NNTP-Posting-Host: ool-18b80c4e.dyn.optonline.net 24.184.12.78
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.adonet:65860
| X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
|
| Hi Mauricio,
|
| Develop a dataview of the table and then you will get the index. Here's
how
| it works:
|
| Dim arrayseeks(0) As Object
|
| Dim ifinds As Integer
|
| Dim vues As New DataView(dsshipcode.Tables(0))
|
| vues.Sort = "shipcode"
|
| Dim irow as datarow
|
| For Each irow In dsshipcode.Tables(0).Rows
|
| arrayseeks(0) = "12345"
|
| ifinds = vues.Find(arrayseeks)
|
| If ifinds <> -1 Then ' ie, found it
|
| mdescrip = vues(ifinds)("descrip")
|
| Else
|
| mdescrip = ""
|
| End If
|
| Next
|
| In this code, ifind in the index of the found row. I suspect this is what
| you were after.
| HTH,
| Bernie Yaeger
|
| | > How can I know the index of a DataRow that belongs to a
| > DataTable ?
| > I have the DataRow and I want your Rows' index.
| >
| > Tanks.
| >
| > Mauricio (Brazil)
|
|
|
 
Back
Top