skyjo
Posts: 1
Joined: 4/7/2005 Status: offline
|
Firstly, Hello to everyone out there! I have been using the "DataItem" class in my project and it has helped tremendously in improving my data management where storing IDs related to a record placed in a combobox or listbox is no longer a problem. I do however have difficulty restoring the values (IDs) from the database onto a listbox. Using the class below I have placed items in a listbox with the following code:
' i is the ID related to strValue, both retrieved from the database
while database is reading row by row
ListBox1.Items.Add(new DataItem(i, strValue))
next database row
end while
Public Class DataItem
Private _id As String
Private _value As String
Public Sub New(ByVal id As String, ByVal value As String)
_id = id
_value = value
End Sub
Public ReadOnly Property ID() As String
Get
Return _id
End Get
End Property
Public ReadOnly Property Value() As String
Get
Return _value
End Get
End Property
Public Overrides Function ToString() As String
Return Value
End Function
The above works flawlessly, and when the user picks a listbox item, I am able to retrieve the associated ID using the following:
sValue = listbox1.Items.Item(listbox1.SelectedIndex).id
The value is then stored in a database table along with other values that are on the windows form. What I need now is to pre-select the item on the listbox when the form loads up with that record. I need a way to relate sValue to the items in the listbox, which has been retrieved from the database record. I have tried the following which does not work because sValue is not the index in the listbox:
listbox1.selectedindex = sValue
'or
listbox1.selecteditem = sValue
Please help me
|