Problem getting updated DisplayMember/ValueMember values for a combo box

I have loaded a combo box with client info using the LoadClientList() below. I've also included code for the cboClients_SelectedIndexChanged event.

 
Private Sub LoadClientList()
        cboClients.Items.Clear()
        Dim strFN As String
        Dim strLN As String
        Dim intID As Integer

        Dim conn As New SqlConnection(connString)
        Dim cmmd As New SqlCommand
        Dim selectStatement As String = "SELECT * FROM Clients"
        Dim selectCommand As New SqlCommand(selectStatement, conn)
        conn.Open()
        Dim reader As SqlDataReader = selectCommand.ExecuteReader(CommandBehavior.SingleResult)
        Do While reader.Read
            intID = CInt(reader(0))
            strFN = reader(1).ToString
            strLN = reader(2).ToString
            cboClients.Items.Add(strFN & " " & strLN)
            With cboClients
                .DisplayMember = strFN & " " & strLN
                .ValueMember = intID.ToString
            End With
        Loop
        conn.Close()
        cboClients.SelectedIndex = -1
    End Sub

    Private Sub cboClients_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboClients.SelectedIndexChanged
        Dim strX As String = cboClients.DisplayMember   'this is the Client Fname & Lname
        Dim strY As String = cboClients.ValueMember     'this is the ClientID   'this is the value I need to get
        Dim strZ As String = cboClients.SelectedItem.ToString   'this is the Client Fname & Lname
        Dim intX As Integer = cboClients.SelectedIndex          'this is the cbo index
        'getClientInfo(CInt(strY))
    End Sub

Open in new window


If I put a breakpoint on line 21 I can see correct values being entered into the DisplayMember/ValueMember properties. However, when I click on a different item in the combo box, the SelectedItem & SelectedIndex properties change (lines 30 & 31), but the values for DisplayMember & ValueMember (lines 28 & 29)retain the values of the last client entered into the combo box.

I need the DisplayMember & ValueMember properties to update!

Thanks for any help.
LezlyPrimeAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Carl TawnConnect With a Mentor Systems and Integration DeveloperCommented:
Instead of:
        Dim strX As String = cboClients.DisplayMember   'this should be the Client Fname & Lname
        Dim strY As String = cboClients.ValueMember     'this shouuld be the ClientID   'this is the value I need to get

Open in new window

You would use:
Dim item As ValueDescriptionPair = CType(cboClients.SelectedItem, ValueDescriptionPair)
Dim strX As String = item.Value;
Dim strY As String = item.Description

Open in new window

Although creating the separate ValueDescription class is totally unnecessary because you can use the ListItem class i demonstrated in my earlier post. Creating your own name/value pair class makes sense in a WinForms app because there is no ListItem equivalent, but for ASP.Net it is totally redundant.
0
 
Mrugesh1Commented:
Tray below statements...

   
cboClients.Items[cboClients.SelectedIndex].Text // Gives Display Value
cboClients.Items[cboClients.SelectedIndex].Value  // Gives Value Member

Open in new window

0
 
Carl TawnSystems and Integration DeveloperCommented:
You're problem stems from misunderstanding what it going on. DisplayMember and ValueMember are supposed to be set to field names to be used when databinding. They do not store arbitary pieces of data as you are attempting to do. Instead you need to populate your dropdown as a series of ListItem objects:
Private Sub LoadClientList()
        cboClients.Items.Clear()
        Dim strFN As String
        Dim strLN As String
        Dim intID As Integer

        Dim conn As New SqlConnection(connString)
        Dim cmmd As New SqlCommand
        Dim selectStatement As String = "SELECT * FROM Clients"
        Dim selectCommand As New SqlCommand(selectStatement, conn)
        conn.Open()
        Dim reader As SqlDataReader = selectCommand.ExecuteReader(CommandBehavior.SingleResult)
        Do While reader.Read
            intID = CInt(reader(0))
            strFN = reader(1).ToString
            strLN = reader(2).ToString
            cboClients.Items.Add(New ListItem(strFN & " " & strLN, intID.ToString()))
        Loop
        conn.Close()
        cboClients.SelectedIndex = -1
    End Sub

Open in new window

The in your SelectedIndexChanged you access the currently selected value through the SelectedValue property:
    Private Sub cboClients_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboClients.SelectedIndexChanged
           Dim id As Integer = CType(cboClients.SelectedValue, Integer)

    End Sub

Open in new window

0
 
LezlyPrimeAuthor Commented:
http://www.mgbrown.com/PermaLink37.aspx

I was originally trying to follow the suggestion in the link above, in the first post under Comments. He stores the data in an array & then sets the array as the data source. Like you stated, the values for the DisplayMember/ValueMember properties are field names.

My code is below. The array is loading & displaying the client names fine. However, lines 31 & 32 are always "Display" & "Value."  How do I reference the item to get the ClientID value?

 
Private Sub LoadClientList()
        Dim aryClients As New ArrayList
        cboClients.Items.Clear()
        Dim conn As New SqlConnection(connString)
        Dim cmmd As New SqlCommand
        Dim selectStatement As String = "SELECT * FROM Clients"
        Dim strFN As String
        Dim strLN As String
        Dim intID As Integer
        Dim selectCommand As New SqlCommand(selectStatement, conn)
        conn.Open()
        Dim reader As SqlDataReader = selectCommand.ExecuteReader(CommandBehavior.SingleResult)

        Do While reader.Read
            intID = CInt(reader(0))
            strFN = reader(1).ToString
            strLN = reader(2).ToString
            aryClients.Add(New ValueDescriptionPair(intID.ToString, (strFN & " " & strLN)))
        Loop
        conn.Close()
        
        With cboClients
            .DataSource = aryClients
            .DisplayMember = "Description"
            .ValueMember = "Value"
        End With
        cboClients.SelectedIndex = -1
    End Sub

    Private Sub cboClients_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboClients.SelectedIndexChanged
        Dim strX As String = cboClients.DisplayMember   'this should be the Client Fname & Lname
        Dim strY As String = cboClients.ValueMember     'this shouuld be the ClientID   'this is the value I need to get
        'getClientInfo(CInt(strY))
    End Sub

**********************
Public Class ValueDescriptionPair

    Private m_Value As Object
    Private m_Description As String

    Public ReadOnly Property Value() As Object
        Get
            Return m_Value
        End Get
    End Property

    Public ReadOnly Property Description() As String
        Get
            Return m_Description
        End Get
    End Property

    Public Sub New(ByVal NewValue As Object, ByVal NewDescription As String)
        m_Value = NewValue
        m_Description = NewDescription
    End Sub

    Public Overrides Function ToString() As String
        Return m_Description
    End Function

End Class

Open in new window

0
 
LezlyPrimeAuthor Commented:
Thank you! This works perfectly & sheds light on how that ValueDescriptionPair works.

I want to note that this solution is not the solution to the original post, but rather to my second post, where there is a link & code to an entirely different way of filling the combo box with two pieces of data.
0
All Courses

From novice to tech pro — start learning today.