Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Capture record index from datarow or datatable? 1

Status
Not open for further replies.

CraigBest

Programmer
Aug 1, 2001
545
US
Having a brain cramp here...

I am accessing a dataset with a data table called "lineitem". There will probably be more than one record in this table, and I need to execute some processing against one of them, specified by the line item number (sLINum).

So what I want to do is iterate through the table and look for the record with the same line item number, then capture the index value so I can use it when processing the right record going forward.

But I can't figure out how to get to the index value. I've included some sample code below to show mmy thought process:


Public Sub Foo(dsLineItem as DataSet, sLINum As String)

Dim dtLI As DataTable
Dim drLI As DataRow
Dim idxLI As Short

dtLI = dsLineItem.Tables("lineitem")

For Each drLI In dtLI.Rows
If drLI("LineItemNum").ToString = sLINum Then
idxLI = drli.<capture index value here?>
Exit For
End If
Next


Any help (or an alternate way to do this) would be appreiciated. Thanks.


CraigHartz
 
Thanks, but according to the IDE IndexOf is not a property of the Rows property. I wish it were!


CraigHartz
 
BTW, the index is an Integer (not a Short).
 
Is not a Short also an integer (just shorter/smaller)? I read that short should be used in place of Integer when using small values (particularly under 100)...


CraigHartz
 
May I ask what you need the index for when you already have the actual row?

 
Might this work?

dtLI = dsLineItem.Tables("lineitem")

Dim nIndex as Integer=0
For Each drLI In dtLI.Rows
If drLI("LineItemNum").ToString = sLINum Then
idxLI = nIndex
Exit For
End If
nIndex += 1
Next
 
<< May I ask what you need the index for when you already have the actual row? >>

Has to do with accessing the fields in the row. I'm using the method dtLI.Rows(X)("Field_Name").ToText to access the fields, and I need to place the index where X is when dealing with multiple rows in a table.

If there's an easier way to do this where I don't need the index value I'm all ears.

I'll give the other method a try -- thanks



CraigHartz
 
Thanks for the second snippet -- it passes the editor's rules, I'll see in a few days if it actually does what I need it to do -- looks like it will, though. Thanks again.


CraigHartz
 
if all you need to do is to access the fields in the row, I can't see why you don't just use a variable that holds a reference to the row. I would recommend that you do not use the index (DataTable contents may change etc.).

Dim drLineItemNum as DataRow

For Each drLI In dtLI.Rows
If drLI("LineItemNum").ToString = sLINum Then
drLineItemNum = drli
Exit For
End If
Next

if not drLineItemNum Is Nothing then
'/// Access the row values here
MessageBox.Show(drLineItemNum("Field_Name"))
end if

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top