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.

Re: Getting RowIndex from a DataTable by Jon

Jon
Mon Aug 23 06:46:39 CDT 2004

Tomer <Tomer@dannet.co.il> wrote:
> 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(...)?

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: Getting RowIndex from a DataTable by Tomer

Tomer
Mon Aug 23 08:58:00 CDT 2004

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" <jon.skeet@peramon.com>
To: "Tomer" <Tomer@dannet.co.il>
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[i] == foundRow)
{
...
}
}

Jon



Re: Getting RowIndex from a DataTable by Jon

Jon
Mon Aug 23 08:43:47 CDT 2004

Tomer <Tomer@dannet.co.il> wrote:
> 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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: Getting RowIndex from a DataTable by Tomer

Tomer
Wed Aug 25 02:14:05 CDT 2004

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]"" <ilyatum@online.microsoft.com> wrote in message
news:PQIgUoTiEHA.2632@cpmsftngxa10.phx.gbl...
> 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.
> --------------------
> > From: "Tomer" <Tomer@dannet.co.il>
> > References: <Oms0cQQiEHA.4056@TK2MSFTNGP09.phx.gbl>
> <MPG.1b93e97ab0a0ce9298b265@msnews.microsoft.com>
> > 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: <eJDO7ARiEHA.1040@TK2MSFTNGP09.phx.gbl>
> > 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" <jon.skeet@peramon.com>
> > To: "Tomer" <Tomer@dannet.co.il>
> > 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[i] == foundRow)
> > {
> > ...
> > }
> > }
> >
> > Jon
> >
> >
> >
>



Re: Getting RowIndex from a DataTable by Andy

Andy
Wed Aug 25 11:16:13 CDT 2004

"Tomer" <Tomer@dannet.co.il> wrote in message
news:OLvUhomiEHA.2992@TK2MSFTNGP12.phx.gbl...
> 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



Re: Getting RowIndex from a DataTable by ilyatum

ilyatum
Wed Aug 25 14:02:31 CDT 2004

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.
--------------------
> From: "Tomer" <Tomer@dannet.co.il>
> References: <Oms0cQQiEHA.4056@TK2MSFTNGP09.phx.gbl>
<MPG.1b93e97ab0a0ce9298b265@msnews.microsoft.com>
<eJDO7ARiEHA.1040@TK2MSFTNGP09.phx.gbl>
<PQIgUoTiEHA.2632@cpmsftngxa10.phx.gbl>
> 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: <OLvUhomiEHA.2992@TK2MSFTNGP12.phx.gbl>
> 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