Results 1 to 16 of 16

Thread: [RESOLVED] [2008] How to get Itemdata of combobox item

  1. #1

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Resolved [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

  2. #2

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Dim items As New List(Of ComboBoxItem)
    2.  
    3. While myDataReader.Read()
    4.     items.Add(New ComboListData(myDataReader.GetString(1), myDataReader.GetString(0)))
    5. End While
    6.  
    7. Me.cboClass.DisplayMember = "Name"
    8. Me.cboClass.ValueMember = "ItemData"
    9. Me.cboClass.DataSource = items
    You then get the ItemData value that corresponds to the selected Name value like so:
    vb.net Code:
    1. Dim itemData As String = CStr(Me.cboClass.SelectedValue)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    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

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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".
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    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.

  8. #8

  9. #9

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    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.

  10. #10

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    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

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    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

  13. #13

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] How to get Itemdata of combobox item

    vb.net Code:
    1. For Each item As ComboListData In items
    2.     MessageBox.Show(item.ItemData.ToString())
    3. Next
    The 'items' in the above code could be the generic List I suggested earlier, or it could be the ComboBox's Items collection.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: [2008] How to get Itemdata of combobox item

    thanks alot guys, it is working great . I appreciate the help

  16. #16
    New Member
    Join Date
    Aug 2010
    Posts
    2

    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
  •  



Click Here to Expand Forum to Full Width