hi experts

During my development progress i was stuck up with a problem.
I used a class MyList and with the Help of this MyList the combo box named cboCity was filled with City Code(like 1001,1002,etc..) as Value member and City Name (like Canada,California etc...) as display member.

Public Class MyList
    Private _dispName As String
    Private _valMember As Integer
    Public Sub New()
        _dispName = ""
        _valMember = 0
    End Sub
    Public Sub New(ByVal dispName As String, ByVal valMember As Integer)
        _dispName = dispName
        _valMember = valMember
    End Sub
    Public Property DisplayName() As String
        Get
            Return _dispName
        End Get
        Set(ByVal value As String)
            _dispName = value
        End Set
    End Property
    Public Property ValueMember() As Integer
        Get
            Return _valMember
        End Get
        Set(ByVal value As Integer)
            _valMember = value
        End Set
    End Property
    Public Overrides Function ToString() As String
        Return _dispName
    End Function
End Class
Dim _MyList as MyList
    _MyList = New MyList("Canada",1001)
    _MyList = New MyList("California",1002)
    _MyList = New MyList("Sydney",1003)
    _MyList = New MyList("Melbourne",1004)
    cbo.Items.Add(_MyList)

The value members 1001,1002,1003,1004 get inserted as per cbocity(combo box)display name selection , into the database and during the retrieval of the record the value member is obtained and based on that for eg:when i get 1001 the selected item should be Canada.I tried many ways while fetching the record and set the selected item but all fails . Please kindly help me in providing the solution.

Dani AI

Generated

— your approach of putting objects into the ComboBox is fine; the missing piece is how the ComboBox is bound and how you tell it which item to pick when you read the numeric code back from the database. is right to ask how you bind the list. Two reliable options follow.

Bind a list and use SelectedValue (recommended)

Populate a typed list (List(Of MyList) or BindingList(Of MyList)), set DisplayMember/ValueMember, then assign SelectedValue to the DB code. SelectedValue only works when the ComboBox has a DataSource.

' after you populate "cities" with MyList objects:
cboCity.DataSource = cities
cboCity.DisplayMember = "DisplayName"
cboCity.ValueMember = "ValueMember"
cboCity.SelectedValue = dbValue  ' dbValue is the integer you read from DB

If you already used Items.Add (no DataSource)

Search the Items collection for the object whose numeric property matches the DB value and set SelectedIndex or SelectedItem.

For i As Integer = 0 To cboCity.Items.Count - 1
    Dim it = TryCast(cboCity.Items(i), MyList)
    If it IsNot Nothing AndAlso it.ValueMember = dbValue Then
        cboCity.SelectedIndex = i
        Exit For
    End If
Next

Troubleshooting tips

  • Make the selection after the ComboBox is fully populated. If population is async, marshal the selection back to the UI thread.
  • Ensure types match (Integer vs String) when you compare.
  • DisplayMember/ValueMember strings must match your property names. SelectedValue uses reflection on the bound objects, so DataSource must be set first.
  • Use breakpoints to inspect cboCity.Items and confirm each item’s ValueMember.

Where is ur code to bind the records to combo? how ur setting the display and value member?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.