finding a row index

B

Bernie Yaeger

I know how to use various .find methods to identify a row index, but I want
to do the same for 'bookmark' type purposes in a simple datatable loop. For
example,

Dim dainvdtmcrd As New SqlDataAdapter("select imcacct from invdtmcr order by
imcacct", oconn)
Dim dsinvdtmcrd As New DataSet("invdtmcrd")

dainvdtmcrd.Fill(dsinvdtmcrd, "invdtmcrd")

dim irow as Datarow

dim x as integer

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

x = ???????

Next

The question marks represent the code I don't know: how can I assign from
irow (or the dataset or datatable itself) to xx such that xx would have the
index of each row. What I need to do is code beneath the 'next' to certain
rows.

Tx for any help.

Bernie Yaeger
 
N

Neil McKechnie

Why do you need an index into the rows? If you want the
equivalent of a bookmark then can't you use a reference to
the row itself?

For Example:

dim irow as Datarow

dim bookmarks as new collection

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

If <row is to be bookmarked> Then
bookmarks.add(irow)
End If

Next

Alternatively, use an index into the Rows collection
instead of ForEach, as below:

For i = 0 to dsinvdtmcrd.Tables(0).Rows.Count-1
If <row is to be bookmarked> Then
x = i
End If
Next i


Neil.
 
H

Hussein Abuthuraya[MSFT]

Hi Bernie,

To accomplish this, you need to create a DataView object from your table, Sort it then do a Find based on a key value. The following code demonstrates that:

da1.Fill(ds, "Customers")

Dim irow As DataRowView
Dim x As Integer

Dim dv As DataView = ds.Tables(0).DefaultView
dv.Sort = "CustomerID"
For Each irow In dv
x = dv.Find(CType(irow("Customerid"), Object))
MessageBox.Show(x)
Next

or simply

Dim vals(0) As Object

Dim dv As DataView = ds.Tables(0).DefaultView
dv.Sort = "CustomerID"
For Each irow In dv
vals(0) = irow("Customerid")
x = dv.Find(vals(0))
MessageBox.Show(x)
Next

I hope this helps!


Thanks,
Hussein Abuthuraya
Microsoft Developer Support

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

Are you secure? For information about the Microsoft Strategic Technology Protection Program and to order your FREE Security Tool Kit, please visit
http://www.microsoft.com/security.
 

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