Finding a record in a dataset

VonEhle

Active member
Joined
Feb 20, 2006
Messages
26
Programming Experience
Beginner
I'm trying to figure out a way to find a row record in a dataset. There is a field in the table called "ID". I want to be able to give it a number and have it display the info from that row.

Here's what I've been trying...

Me.BindingContext(DsResults1, "tbl_Results").Position = 0
Do Until drCurrent.Item("ID") Is txtGameID.Text
drCurrent = DsResults1.tbl_Results.Rows(pintDataRow)
pintDataRow += 1
Loop

Thanks for your help.
 
One thing I dont get is the way you're using the 'Is' keyword. In my understanding, that keyword is used to check if two references refer to the same object, so if you're trying to check if two strings or numbers have the same value then this wont work (except under special circumstances). In this case wouldn't using the '=' operator be correct?

The way you use your loop is rather odd as well (well, i learned how to use loops and that differently and am partial to using 'for' loops over any other :) ). I code in my own way but this is how I would handle the looping:
Dim drCurrent As DataRow
For Each drCurrent In DsResults1.tbl_Results.Rows
If drCurrent.Item("ID") = txtGameID.Text Then
Exit For
End If
Next
After exiting the loop, the drCurrent variable already has the values for the row you were searching for so now you just have to access its items.

*nod* ja, if its the primary key use the Find method to get the row
 
VB.NET:
Dim DV As DataView
Dim I As Integer
DV = New DataView(DsDataSet1.Tables("TableName"))
DV.Sort = ("ID")
I = DV.Find(txtGameID.Text)
Me.BindingContext(DsDataSet1, "TableName").Position = I
You find the location of the matching record and then move to that location to access the other fields. Anything bound to the dataset will display the found row once you move there.
 
Back
Top