ADO.NET, need to get index of datarow

  • Thread starter Thread starter BB
  • Start date Start date
B

BB

Hello all,

I am using the currency manager in VB to navigate a
dataset. I know how to use .position to loop through the
rows, but what I want to do is *get* the position of the
row I just added/updated, so that I can reposition the
currency manager there sometime later.

Seems like after I add a detached datarow to the
datatable, that index should be sitting there somewhere
for me to grab, but I don't see where that is in either
the currency manager, the dataset itself, etc. I can
always redo the i/o to re-retrieve the datarow, but that
seems like a waste.

Any ideas?

TIA,

BB
 
Hi BB,

The currency manager gives you the current row on your control
(so I thought that you don't have to use a loop for updating)
The last added row in a dataset is as far as I know always
dataset.tables(x).rows.count - 1

And if you want to know a row you are updating using a loop, why not just
use a loop like
\\\
dim i as integer
for i = 1 to dataset.tables(x).rows.count - 1
do something with the found row
datset.tables(x).rows(i).item("myitem") etc
if found lastrow = i
exit for
next
///
I hope this was where you was after?

Cor
 
* "BB said:
I am using the currency manager in VB to navigate a
dataset. I know how to use .position to loop through the
rows, but what I want to do is *get* the position of the
row I just added/updated, so that I can reposition the
currency manager there sometime later.

Seems like after I add a detached datarow to the
datatable, that index should be sitting there somewhere
for me to grab, but I don't see where that is in either
the currency manager, the dataset itself, etc. I can
always redo the i/o to re-retrieve the datarow, but that
seems like a waste.

..NET + Database group:

<
Web interface:

<http://msdn.microsoft.com/newsgroup...roup=microsoft.public.dotnet.framework.adonet>
 
Hi BB,

Not too many people know how to get a row index using ado .net and many will
tell you any number of reasons why you don't need it - but you and I know
that you do.

I wrote a function that will do this for you by returning the index of the
row in question. Note that in the code below 'functions' is a class in a
..dll called 'imcfunctionlib'.
Public Function findindex(ByVal dt As DataTable, ByVal dr As DataRow) As
Integer

' signature:

' dim funcs as new imcfunctionlib.functions

' dim xint as integer

' xint = funcs.findindex(dsmanifest.tables(0),irow)

' when you call findindex inside a loop the current irow index is returned
with

' idex = findindex(ods.Tables(0), irow)

' at the same time, this

' MessageBox.Show(irow("imcacct"))

' equals

' MessageBox.Show(ods.Tables(0).Rows(idex)("imcacct"))

' once outside the loop - see dataworks for more info

Dim i As Integer

For i = 0 To dt.Rows.Count - 1

If Object.ReferenceEquals(dt.Rows(i), dr) Then

Return i

End If

Next

Return -1

End Function

HTH,

Bernie Yaeger
 
Bernie, this was a big help. Thanks.
-----Original Message-----
Hi BB,

Not too many people know how to get a row index using ado .net and many will
tell you any number of reasons why you don't need it - but you and I know
that you do.

I wrote a function that will do this for you by returning the index of the
row in question. Note that in the code
below 'functions' is a class in a
 
Also here can be used search abilities of an ArrayList container that presented by List property of DataRowCollection(IndexOf, BinarySearch..). DataRowCollection is returned by DataTable->Rows property. To access protected member without compiler's modifications lets doing something like this:

#pragma warning( disable : 4669)

public __gc class ListPropertyPublicRowCollection: public System::Data::DataRowCollection
{
public: inline static GetRowIndex(DataRowCollection* rc, DataRow* dr)

{ return reinterpret_cast<ListPropertyPublicRowCollection*>(rc)->List->IndexOf(dr);}
};


or like this

public __gc class ListPropertyPublicRowCollection: public System::Data::DataRowCollection
{
public: __property virtual ArrayList* get_List(){return DataRowCollection::get_List();}

};

...

reinterpret_cast<ListPropertyPublicRowCollection*>(myDataTable->Rows)->List->IndexOf(myDataRow);

choose that you like.
 

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

Back
Top