|
-
Dec 20th, 2007, 07:43 PM
#1
Thread Starter
Lively Member
[RESOLVED] [2005] Selecting a specific Row from a DataTable.
Hi all. I could use a little help with selecting a specific DataRow from a DataTable.
I am using the following code to select specific DataRows from a DataTable and this works fine, but many times I am looking for only a single row of data (there is not a possibility of multiple rows) and I was wondering if there is a better method to select a single row rather than the For-Next loop which seams set up specifically for multiple rows.
Or… am I just making too much of this? In this example, the DataTable does not have any controls bound to it.
Code:
For Each row As ScoreboardDataSet.tbl_StudentPhysicalDataRow In Me.ScoreboardDataSet.tbl_StudentPhysicalData.Select(String.Format("StudentID LIKE '{0}'", GWrestlerID))
If Not row.LastWeighInWeight = Nothing Then MyVariable = row.LastWeighInWeight
Next Mrow
Thanks for your help.
-
Dec 20th, 2007, 09:03 PM
#2
Re: [2005] Selecting a specific Row from a DataTable.
If you want to find a single row by its primary key the you would use the DataRowCollection.Find method, e.g.
vb.net Code:
Dim row As DataRow = table.Rows.Find(id)
Find also accepts an array of values for composite keys.
If you want to find one or more rows based on one or more non-key columns you would use the DataTable.Select method, as you are. If you know for a fact that there's only going to be one row then there's no need to use a loop. You'd just get the row at index 0 straight off, e.g.
vb.net Code:
Dim row As DataRow = table.Select(String.Format("Name = '{0}'", name))(0)
Note also that if you are looking for a row where a field value is equal to a specific value then you should use '=', not 'LIKE'. If you are not using wildcards then using 'LIKE' is pointless.
-
Dec 21st, 2007, 07:16 PM
#3
Thread Starter
Lively Member
Re: [2005] Selecting a specific Row from a DataTable.
Perfect... Thanks JM.
I have been in the MSDN Lib reading about the DataTable Methods, but this really puts a bow on the information provided there.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|