Finding the Number of a New Row

  • Thread starter Gregory A Greenman
  • Start date
G

Gregory A Greenman

If I have a table ("tbl") in a dataset ("ds") with these three
fields:

pk - autoincrement, seed = -1, step = -1
fld0
fld1

and I add a new row, how can I find its row number for the
default view?

Here's some sample code:

---------------
dim dr as datarow
dim lngIndex as long

dr = ds.tables("tbl").newrow
dr("fld0") = 100
dr("fld1") = "Hello World"
ds.tables("tbl").rows.add(dr)

lngIndex = getNumberOfTheRowIJustAdded
---------------

What do I replace "getNumberOfTheRowIJustAdded" with in order to
get the new row number if I want it for a default view?

Unfortunately, the find method returns the row itself, rather
than its number. Since I can't see any way to determine the row
number from that, it doesn't solve my problem. The only solution
I see is to iterate through the data until I have a match.

Also, am I correct in assuming that the number of the newly added
row is the same as ds.tables("tbl").rows.count - 1 if I am not
using a view?

Thanks for any help.
 
M

Miha Markic [MVP C#]

Hi Gregory,

Gregory A Greenman said:
If I have a table ("tbl") in a dataset ("ds") with these three
fields:

pk - autoincrement, seed = -1, step = -1
fld0
fld1

and I add a new row, how can I find its row number for the
default view?

Here's some sample code:

---------------
dim dr as datarow
dim lngIndex as long

dr = ds.tables("tbl").newrow
dr("fld0") = 100
dr("fld1") = "Hello World"
ds.tables("tbl").rows.add(dr)

lngIndex = getNumberOfTheRowIJustAdded

You should use DataView.Find() method as position in DataView is bound to
current sorting value.
Or, iterate through all DataView rows and check their Row property against
your new row).
Unfortunately, the find method returns the row itself, rather
than its number. Since I can't see any way to determine the row
number from that, it doesn't solve my problem. The only solution
I see is to iterate through the data until I have a match.

Also, am I correct in assuming that the number of the newly added
row is the same as ds.tables("tbl").rows.count - 1 if I am not
using a view?

Yes. The row is added at the end.
 
G

Gregory A Greenman

"Miha Markic said:
Hi Gregory,

Hello





You should use DataView.Find() method as position in DataView is bound to
current sorting value.


Unfortunately, Dataview.Find only works if the sort is unique,
which may not be the case for my application.




Or, iterate through all DataView rows and check their Row property against
your new row).


Actually, as I was lying awake suffering from insomnia, I had an
idea that seems to work. My table has another field called
"sort", which is a user editable sort field. So, when I add a new
row, I'll just make sure it sorts to the beginning. Here's some
sample code:

---------------
dim dr as datarow
dim lngIndex as long

dr = ds.tables("tbl").newrow
dr("fld0") = 100
dr("fld1") = "Hello World"

with ds.tables("tbl").defaultview
dr("sort") = iif(.count = 0, 0, .Item(0).Item("sort") -1)
end with

ds.tables("tbl").rows.add(dr)

lngIndex = 0
---------------

I've tested it and that seems to satisfy my needs. Of course,
this also means that sort is unique (for the new record), so I
could do a find, but since I know it's first, I don't need to.

Many thanks for your help. Now if you can just suggest a cure for
insomnia, you'll have solved all my problems.
 

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