[RESOLVED] [2008] How to get Itemdata of combobox item
Hi Guys,
I currently have this code to load a combobox. I also have a class to set the itemdata and text for each item.
my problem is that when the form loads I have another query that selects a classid for a student. I want to compare the itemdata for each item in the combobox against the current classid for that pupil and then set the selectedindex of the combo where that classid is. How can I do it. Please help.
Code:
Private Sub LoadClassCombo()
Dim myDataReader As MySqlDataReader
Using con As New MySqlConnection
Dim strsql As String = "SELECT classid,CONCAT_WS(' ',gradelevel,class) from classes ORDER BY Gradelevel;"
Dim cmd As New MySqlCommand
With cmd
.Connection = New MySqlConnection(myConnStr)
.Connection.Open()
.CommandText = strsql
myDataReader = .ExecuteReader
End With
With cboClass
.Items.Clear()
Do While myDataReader.Read
.Items.Add(New ComboListData(myDataReader.GetString(1), myDataReader.GetString(0)))
Loop
End With
End Using
End Sub
class:
Code:
Public Class ComboListData
Private sName As String
Private iID As Long
Public Sub New()
sName = ""
iID = 0
End Sub
Public Sub New(ByVal Name As String, ByVal ID As Long)
sName = Name
iID = ID
End Sub
Public Property Name() As String
Get
Return sName
End Get
Set(ByVal sValue As String)
sName = sValue
End Set
End Property
Public Property ItemData() As Long
Get
Return iID
End Get
Set(ByVal iValue As Long)
iID = iValue
End Set
End Property
Public Overrides Function ToString() As String
Return sName
End Function
End Class
Re: [2008] How to get Itemdata of combobox item
vb.net Code:
TextBox1.Text = CType(cboClass.SelectedItem, ComboListData).ItemData
Re: [2008] How to get Itemdata of combobox item
Instead of adding your ComboListItems to the ComboBox directly, add them to a List or BindingList, then bind that to the ComboBox.
vb.net Code:
Dim items As New List(Of ComboBoxItem)
While myDataReader.Read()
items.Add(New ComboListData(myDataReader.GetString(1), myDataReader.GetString(0)))
End While
Me.cboClass.DisplayMember = "Name"
Me.cboClass.ValueMember = "ItemData"
Me.cboClass.DataSource = items
You then get the ItemData value that corresponds to the selected Name value like so:
vb.net Code:
Dim itemData As String = CStr(Me.cboClass.SelectedValue)
Re: [2008] How to get Itemdata of combobox item
Hi Deepak,
Thanks for the reply;) . I tried this but I get an error:
Argument 'Prompt' cannot be converted to type 'String'.
Code:
For j = 0 To cboClass.Items.Count
If CurClassId = CType(cboClass.Items(j), ComboListData).ItemData Then
End If
Next
Re: [2008] How to get Itemdata of combobox item
Quote:
Originally Posted by Nitesh
Hi Deepak,
Thanks for the reply;) . I tried this but I get an error:
Argument 'Prompt' cannot be converted to type 'String'.
Code:
For j = 0 To cboClass.Items.Count
If CurClassId = CType(cboClass.Items(j), ComboListData).ItemData Then
End If
Next
You didn't get it on any of that code because there's no method there with an parameter named "Prompt".
Re: [2008] How to get Itemdata of combobox item
Hey, I just realised, you've declared your ItemData property as type Long and your ComboBoxItem constructor has a corresponding Long parameter, yet when you create the ComboBoxItem objects you're specifically getting a String and passing it to that parameter.
Code:
.Items.Add(New ComboListData(myDataReader.GetString(1), myDataReader.GetString(0)))
What's up with that?
Re: [2008] How to get Itemdata of combobox item
Hi jmcilhinney,
When I try this line of code:
Code:
Dim items As New List(Of ComboBoxItem)
the ComboBoxItem has that blue squiggly line under it. It isn't recognised. Am i doing something wrong.
Re: [2008] How to get Itemdata of combobox item
Your class name is ComboListData and not ComboBoxItem and the error you are talking about is not with the code i posted. I am sure there something wrong at different place.
Re: [2008] How to get Itemdata of combobox item
Code:
Hey, I just realised, you've declared your ItemData property as type Long and your ComboBoxItem constructor has a corresponding Long parameter, yet when you create the ComboBoxItem objects you're specifically getting a String and passing it to that parameter.
oops:o
plz correct it for me.
Re: [2008] How to get Itemdata of combobox item
sorry guys, but I need to loop through all the itemdata. How can I do that
Re: [2008] How to get Itemdata of combobox item
Whether you're doing as you were originally or you've changed to what I suggested, you have a collection of ComboListData items. Just use a For Each loop to visit each one and get its ItemData property.
Re: [2008] How to get Itemdata of combobox item
please, please give me an example of how i would do the for each using the list.
I tried this but I can't get it to work:
Code:
For j = 0 To items.Count
If CLng(items(j).ItemData) = CurClassId Then
cboClass.SelectedIndex = j
End If
Next
This is probably the last step. BTW I have switched to your suggestion jmcilhinney:afrog:
Re: [2008] How to get Itemdata of combobox item
vb.net Code:
For j = 0 To items.Count
If CType(items(j), ComboListData).ItemData = CurClassId Then
cboClass.SelectedIndex = j
End If
Next
Re: [2008] How to get Itemdata of combobox item
vb.net Code:
For Each item As ComboListData In items
MessageBox.Show(item.ItemData.ToString())
Next
The 'items' in the above code could be the generic List I suggested earlier, or it could be the ComboBox's Items collection.
Re: [2008] How to get Itemdata of combobox item
thanks alot guys, it is working great:afrog: . I appreciate the help:wave:
Re: [RESOLVED] [2008] How to get Itemdata of combobox item
How to extract itemdata of the selected Item, without looping. Thanks.