|
-
Jul 21st, 2008, 01:40 AM
#1
Thread Starter
PowerPoster
[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
-
Jul 21st, 2008, 01:57 AM
#2
Re: [2008] How to get Itemdata of combobox item
vb.net Code:
TextBox1.Text = CType(cboClass.SelectedItem, ComboListData).ItemData
-
Jul 21st, 2008, 02:07 AM
#3
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)
-
Jul 21st, 2008, 02:16 AM
#4
Thread Starter
PowerPoster
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
-
Jul 21st, 2008, 02:18 AM
#5
Re: [2008] How to get Itemdata of combobox item
 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".
-
Jul 21st, 2008, 02:22 AM
#6
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?
-
Jul 21st, 2008, 02:23 AM
#7
Thread Starter
PowerPoster
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.
-
Jul 21st, 2008, 02:26 AM
#8
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.
-
Jul 21st, 2008, 02:27 AM
#9
Thread Starter
PowerPoster
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
plz correct it for me.
-
Jul 21st, 2008, 02:35 AM
#10
Thread Starter
PowerPoster
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
-
Jul 21st, 2008, 02:41 AM
#11
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.
-
Jul 21st, 2008, 02:53 AM
#12
Thread Starter
PowerPoster
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
-
Jul 21st, 2008, 03:40 AM
#13
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
-
Jul 21st, 2008, 04:25 AM
#14
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.
-
Jul 21st, 2008, 04:35 AM
#15
Thread Starter
PowerPoster
Re: [2008] How to get Itemdata of combobox item
thanks alot guys, it is working great . I appreciate the help
-
Aug 23rd, 2010, 05:04 AM
#16
New Member
Re: [RESOLVED] [2008] How to get Itemdata of combobox item
How to extract itemdata of the selected Item, without looping. Thanks.
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
|