Getting RowIndex from a DataTable

J

Jon Skeet [C# MVP]

Tomer said:
I need to get the RowIndex of a certain row in a DataTable,
I have the primary key field in a veriable and I need to retrieve the
RowIndex of the curresponding record.

Have you tried DataTable.Rows.Find(...)?
 
T

Tomer

Hi,

I need to get the RowIndex of a certain row in a DataTable,
I have the primary key field in a veriable and I need to retrieve the
RowIndex of the curresponding record.

How can I do that?

Tomer.
 
J

Jon Skeet [C# MVP]

Tomer said:
This method isn't very efficient, its like performing the search twice on
the table, and the first one is therefore unnecessary.
But it seems theres no other way...

Well, you could do the search by yourself, if you know enough about the
primary key. Calling Find() just makes things easier.
 
T

Tomer

This method isn't very efficient, its like performing the search twice on
the table, and the first one is therefore unnecessary.
But it seems theres no other way...


----- Original Message -----
From: "Jon Skeet" <[email protected]>
To: "Tomer" <[email protected]>
Sent: Monday, August 23, 2004 2:40 PM
Subject: RE: Getting RowIndex from a DataTable

This only retrieves a Datarow and does not give me a row
index. I need the row index so that I can use it on a
datagrid linked to the table, so that it will show me the
right record. Same thing with a combobox.

Ah, right.

Well, ComboBox.Items has IndexOf which takes the object that you've
previously found.

Not sure why there isn't an equivalent for DataTable - I suggest just
using Find to get the DataRow, and then using something like:

for (int i=0; i < dataTable.Rows.Count; i++)
{
if (dataTable.Rows == foundRow)
{
...
}
}

Jon
 
I

Ilya Tumanov [MS]

Why exactly you need an index if you have the row itself?

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
Subject: Re: Getting RowIndex from a DataTable
Date: Mon, 23 Aug 2004 15:58:00 +0200
Lines: 36
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1437
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: bzq-165-146.dsl.bezeqint.net 62.219.165.146
Path: cpmsftngxa10.phx.gbl!TK2MSFTFEED01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09
.phx.gbl
Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.framework.compactframework:59835
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

This method isn't very efficient, its like performing the search twice on
the table, and the first one is therefore unnecessary.
But it seems theres no other way...


----- Original Message -----
From: "Jon Skeet" <[email protected]>
To: "Tomer" <[email protected]>
Sent: Monday, August 23, 2004 2:40 PM
Subject: RE: Getting RowIndex from a DataTable

This only retrieves a Datarow and does not give me a row
index. I need the row index so that I can use it on a
datagrid linked to the table, so that it will show me the
right record. Same thing with a combobox.

Ah, right.

Well, ComboBox.Items has IndexOf which takes the object that you've
previously found.

Not sure why there isn't an equivalent for DataTable - I suggest just
using Find to get the DataRow, and then using something like:

for (int i=0; i < dataTable.Rows.Count; i++)
{
if (dataTable.Rows == foundRow)
{
...
}
}

Jon
 
T

Tomer

In order to position a certain record on a grid you will need the row index,
and if you'll perform a search on the datatable you'll get the row it, not
the row index.

Tomer.

"Ilya Tumanov [MS]" said:
Why exactly you need an index if you have the row itself?

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.
Subject: Re: Getting RowIndex from a DataTable
Date: Mon, 23 Aug 2004 15:58:00 +0200
Lines: 36
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1437
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: bzq-165-146.dsl.bezeqint.net 62.219.165.146
Path:
cpmsftngxa10.phx.gbl!TK2MSFTFEED01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09
phx.gbl
Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.framework.compactframework:59835
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

This method isn't very efficient, its like performing the search twice on
the table, and the first one is therefore unnecessary.
But it seems theres no other way...


----- Original Message -----
From: "Jon Skeet" <[email protected]>
To: "Tomer" <[email protected]>
Sent: Monday, August 23, 2004 2:40 PM
Subject: RE: Getting RowIndex from a DataTable

This only retrieves a Datarow and does not give me a row
index. I need the row index so that I can use it on a
datagrid linked to the table, so that it will show me the
right record. Same thing with a combobox.

Ah, right.

Well, ComboBox.Items has IndexOf which takes the object that you've
previously found.

Not sure why there isn't an equivalent for DataTable - I suggest just
using Find to get the DataRow, and then using something like:

for (int i=0; i < dataTable.Rows.Count; i++)
{
if (dataTable.Rows == foundRow)
{
...
}
}

Jon

 
A

Andy Becker

Tomer said:
In order to position a certain record on a grid you will need the row index,
and if you'll perform a search on the datatable you'll get the row it, not
the row index.

Tomer.

It's not going to be pretty, but it can be done. This method assumes that
the datagrid's underlying view is sorted on the PK value, and databinding is
via DataSource and DataMember. The syntax changes slightly if you bind
DataSource directly to a datatable. I hope you don't mind VB:

Dim PKValue As Integer = 42
Dim dv As DataView

dv =
DirectCast(DirectCast(Me.DataGrid1.BindingContext(Me.DataGrid1.DataSource,
Me.DataGrid1.DataMember), CurrencyManager).List, DataView)
dv.Sort = "ItemID"

Dim drv As DataRowView = dv.Item(dv.Find(PKValue))
Me.DataGrid1.CurrentRowIndex =
DirectCast(Me.DataGrid1.BindingContext(Me.DataGrid1.DataSource,
Me.DataGrid1.DataMember), CurrencyManager).List.IndexOf(drv)
Me.DataGrid1.Select(Me.DataGrid1.CurrentRowIndex)

Best Regards,

Andy
 
I

Ilya Tumanov [MS]

So, you have a DataTable bound to a DataGrid and you want to find and
programmatically select specific row in the grid, is that right?

If so, row index in the DataTable won't do you any good as it has nothing
to do with row index in the grid.
DataGrid is bound to the DataView (DataTable.DefaultView or your own
DataView), not to the DataTable itself.
You should use DataView.Find() method which returns index you need.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
<[email protected]>
Subject: Re: Getting RowIndex from a DataTable
Date: Wed, 25 Aug 2004 09:14:05 +0200
Lines: 79
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1437
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: bzq-165-146.dsl.bezeqint.net 62.219.165.146
Path: cpmsftngxa10.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12
.phx.gbl
Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.framework.compactframework:59959
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

In order to position a certain record on a grid you will need the row index,
and if you'll perform a search on the datatable you'll get the row it, not
the row index.

Tomer.

"Ilya Tumanov [MS]" said:
Why exactly you need an index if you have the row itself?

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.
Subject: Re: Getting RowIndex from a DataTable
Date: Mon, 23 Aug 2004 15:58:00 +0200
Lines: 36
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1437
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: bzq-165-146.dsl.bezeqint.net 62.219.165.146
Path:
cpmsftngxa10.phx.gbl!TK2MSFTFEED01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09
phx.gbl
Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.framework.compactframework:59835
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

This method isn't very efficient, its like performing the search twice on
the table, and the first one is therefore unnecessary.
But it seems theres no other way...


----- Original Message -----
From: "Jon Skeet" <[email protected]>
To: "Tomer" <[email protected]>
Sent: Monday, August 23, 2004 2:40 PM
Subject: RE: Getting RowIndex from a DataTable


This only retrieves a Datarow and does not give me a row
index. I need the row index so that I can use it on a
datagrid linked to the table, so that it will show me the
right record. Same thing with a combobox.

Ah, right.

Well, ComboBox.Items has IndexOf which takes the object that you've
previously found.

Not sure why there isn't an equivalent for DataTable - I suggest just
using Find to get the DataRow, and then using something like:

for (int i=0; i < dataTable.Rows.Count; i++)
{
if (dataTable.Rows == foundRow)
{
...
}
}

Jon


 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top