Search code examples
vb.netcomboboxvaluemember

Display members and Value members


Hi I am new to VB platform, Can any one help me to understand the last few lines of code, here I highlight with bold at last, which is not understand or confusing to me. What does that Display member and value number do?

.. cmbcust is the combobox...

Where customer table are having following field.

**Customer_sname** **Customer_code** **Customer_fname**
nokia       1       nokia corp.
samsung     2       samsung corp.
sony        3       sony corp.
Micromax    4       Micromax India corp.

passing custval is nokia, samsung, sony

Public Function customfunc(ByVal custval As String) As DataSet
        Try
            Dim strSQL As String = "select * from customer  where  cust_sname  in (" & custval & ")"
            If Conn.State = ConnectionState.Open Then Conn.Close()
            Conn.Open()
            Dim Adap As New SqlDataAdapter(strSQL, Conn)
            Dim Ds As New DataSet
            Adap.Fill(Ds, "customer")
            ReadINICustomers = Ds
        Catch EXP As Exception
            MsgBox("Error Connecting to Server :" & EXP.Message, MsgBoxStyle.Critical)
        End Try
    End Function


   Public Sub Fillcustomer()
        Dim Lstcust() As String
        Dim Lstcust1 As String
        Lstcust1 = ""
        Lstcust1 = custINIval
        Dim Ds As New DataSet
        Ds = objData.ReadINICustomers(Lstcust1)
        cmbcust.DataSource = Ds.Tables("customer")
        cmbcust.DisplayMember = Ds.Tables("customer").Columns.Item("cust_sname").ToString().Trim()
        cmbcust.ValueMember = Ds.Tables("customer").Columns.Item("cust_code").ToString().Trim()
    End Sub

cmbcust.DisplayMember = Ds.Tables("customer").Columns.Item("cust_sname").ToString().Trim() cmbcust.ValueMember = Ds.Tables("customer").Columns.Item("cust_code").ToString().Trim()


Solution

  • When working in any of the .NET languages, such as VB.NET, the MSDN is your friend. It is the official resource for documentation regarding the languages and all of the types in the .NET Framework. In this case, you are asking about a couple of properties on the ComboBox control. The first thing you should do, then, is to search the MSDN for the ComboBox class. If you do so, you will find this article. It lists all of the members of the class and has a separate article explaining each one. If you scroll down through the list of properties, you will find links to articles for the DisplayMember property and the ValueMember property.

    As those articles describe, the ComboBox control can contain any type of objects in its list of items. If you put something simple like a list of strings into the ComboBox, then its obviously easy for it to determine what to display in the list and what to return for its current value. However, when you place complex custom objects in the ComboBox, it's a more difficult proposition.

    By default, it will display whatever the ToString method returns for each of the objects in its list. However, by setting the DisplayMember property, you can instruct it to use a particular member (such as a Property or Function) of the objects in the list instead of the ToString method. You do so by setting the DisplayMember property to the string name of the objects' member. It then uses reflection to find the member by that name in each of the objects and retrieve its value.

    The ValueMember is very similar, but rather than controlling what gets displayed, it controls what gets returned by the SelectedValue property. By default, the SelectedValue property simply returns the entire object that was selected in the list. However, by setting the ValueMember, you can instruct it to just return the value of one particular member from the object rather than the whole thing.