how to select items in code in a listbox bound to a dataset

tcl4p

Well-known member
Joined
Feb 29, 2008
Messages
48
Programming Experience
1-3
I have a list box that is bound to a datatable, which works fine. The question I have is this. I have a list of values in the form of a sqlDatareader that I want to use to select values in the list box, that is to say if the value from from the datareader matches the value member from the row in the listbox then select the row. I've come up with the following code, but can't find the syntax to utilize the SetSelected method. Could use some help please.

Thanks,
Tom


Private Sub SelectChargeConditions()
Dim rs As SqlDataReader
Dim i As Integer
Dim ConditionID


rs = clsDefendant.GetDefendantChargeConditions
Do While rs.Read()
For Each row As DataRowView In lbConditions.Items
ConditionID = row(lbConditions.ValueMember)
If rs("ID") = ConditionID Then
'Set the list box row to Selected here
End If
Next row
Loop

End Sub
 
John,
thanks for the reply. I was under the impression that since the the listbox was bound to a dataset I had to use the row syntax in order to access the value member property. I did try and use the For Next loops looking for the value, but the only thing I was getting was the Display Member property/Text value in the row. If this is not the case can you provide a snippet of code to loop through the listbox, find the value member for that row and if it equals the value from the datareader highlight/select that row in the list box.

Thanks,
Tom
 
I didn't take notice you were using ValueMember, so the other option is not an option.
VB.NET:
For Each row As DataRowView In lbConditions.Items
=>
VB.NET:
For rowindex = 0 to lbConditions.Items.Count - 1
    Dim row As DataRowView = lbConditions.Items(rowindex)
 
that was the syntax I was looking for.
You mean this?
VB.NET:
For counter [ As datatype ] = start To end [ Step step ]
    [ statements ]
    [ Continue For ]
    [ statements ]
    [ Exit For ]
    [ statements ]
Next [ counter ]
There are various code samples in the For-Next help page also.
 
Back
Top