Results 1 to 9 of 9

Thread: [RESOLVED]Value Member & Display member for combo box

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    166

    Resolved [RESOLVED]Value Member & Display member for combo box

    A query brings back case_ID and Case name

    I want to display case name in the combo box, but when it is selected the value that is passed through to my insert statement i want to be the Case_ID that relates to the case name selected.

    i tried this

    casename.ValueMember = "Case_ID"
    casename.DisplayMember = "Case_Name"

    but when message boxing the display member, it just shows the text. Anyone help?
    Last edited by HelpLaura; Apr 26th, 2007 at 07:52 PM.

  2. #2
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Value Member & Display member for combo box

    Bind the combobox to the case datasource.

    ComboBox1.DataSource = "Your datasource???"
    ComboBox1.DisplayMember = "Case_Name"

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    166

    Resolved [RESOLVED] Re: Value Member & Display member for combo box

    Hi, i tried this but get the error:

    Cannot bind to the new display member.
    Parameter name: newDisplayMember

    the code i used for this was:

    vb Code:
    1. Private Sub frm_communication_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.          Dim Conn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
    3.                                                          "Data Source=" & DBPath & ";")
    4.         Dim Comm As System.Data.OleDb.OleDbCommand
    5.  
    6.         Comm = New System.Data.OleDb.OleDbCommand("SELECT case_id, case_name FROM tbl_case ORDER BY case_name", Conn)
    7.         Dim DA As New System.Data.OleDb.OleDbDataAdapter
    8.         DA.SelectCommand = Comm
    9.  
    10.         Dim DT As DataTable
    11.         DA.Fill(DS)
    12.         DT = DS.Tables(0)
    13.         Dim r As DataRow
    14.         For Each r In DT.Rows
    15.             Dim i As Integer = Me.CaseName.Items.Add(r("case_name"))
    16.         Next
    17.         Comm.Dispose()
    18.         DA.Dispose()
    19.         Conn.Close()
    20.         Conn.Dispose()
    21.         casename.DataSource = DS
    22.         casename.ValueMember = "case_id"
    23.         casename.DisplayMember = "case_name"
    24.         MessageBox.Show(casename.ValueMember.ToString)
    25.     End Sub
    Anyone know why this happpening? Have i just define them in thewring place?
    Last edited by HelpLaura; Apr 26th, 2007 at 07:48 PM. Reason: Resolved

  4. #4
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Value Member & Display member for combo box

    What is "caseName"? if it is a combobox then why are you adding items to it. If you bind it to the DS then it spouse to fill the items automaticly.

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

    Re: Value Member & Display member for combo box

    'DS' is your DataSet. Your DataSet doesn't contain anything named "case_id" or "case_name". Your DataSet contains a DataTable named "tbl_case". That DataTable, which is referred to by the 'DT' variable, contains columns named "case_id" and "case_name". You should be assigning that DataTable to the DataSource. It should look like this:
    vb Code:
    1. casename.ValueMember = "case_id"
    2. casename.DisplayMember = "case_name"
    3. casename.DataSource = DT
    Note that I set the DisplayMember and ValueMember before setting the DataSource.

    Note also that you should get rid of this:
    vb Code:
    1. Dim r As DataRow
    2. For Each r In DT.Rows
    3.     Dim i As Integer = Me.CaseName.Items.Add(r("case_name"))
    4. Next
    If you're binding the data to the control then you don't add the data manually too.

    Finally, while it's not critiacal either it is good practice to ALWAYS specify the name of the table you're filling:
    vb Code:
    1. DA.Fill(DS, "tbl_case")
    And finally again, as far as I can see from that code there's no point you even using a DataSet. You may as well just use a DataTable on its own. Here's a cleaned up version of that code:
    vb Code:
    1. Dim con As New OleDbConnection("connection string here")
    2. Dim adp As New OleDbDataAdapter("SELECT case_id, case_name FROM tbl_case ORDER BY case_name", con)
    3. Dim tbl As New DataTable
    4.  
    5. adp.Fill(tbl)
    6.  
    7. adp.Dispose()
    8. con.Dispose()
    9.  
    10. Me.caseName.DisplayMember = "case_name"
    11. Me.caseName.ValueMember = "case_id"
    12. Me.caseName.DataSource = tbl
    Note also that "casename" is probably not the best name for a ComboBox. That variable name implies that it contains the name of a case. That might be true of a String but not a ComboBox. I'd suggest something that more accurately describes its purpose as a way to select a case name, like "caseNameCombo" or "caseNameSelector".
    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

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    166

    Re: Value Member & Display member for combo box

    Hi guys thanks for your posts on this.

    I have added the code provided ontot he form load event. The combo box is automatically populated with the first value in the combo box. But when i got to display the message box. I still get just case_id rather than the actual number

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

    Re: Value Member & Display member for combo box

    ValueMember is the name of the member of the DataSource from which the values should be drawn. Once you assign a value to it, which in your case is "case_id", that is the value that it will always have.

    As the name suggests, it's the SelectedValue property of the ComboBox that returns the value of the selected item.

    When you get the SelectedValue property it will get the value of the property specified in the ValueMember from the current SelectedItem.
    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

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    166

    Re: Value Member & Display member for combo box

    I get what you mean now.

    This has been a massive help in my project.

    Thanks

  9. #9
    Fanatic Member
    Join Date
    Dec 2003
    Posts
    509

    Re: Value Member & Display member for combo box

    Microsoft recommend that you place them in this order or your liable for up to 40% inefficiency

    datamember
    valuemember
    datasource

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