none
ComboBox - display DisplayMember instead of ValueMember RRS feed

  • Question

  • using vb.net 2010 and WPF

    I've got an Access 2007 table with names of people and the unique ID # of their sponsoring agencies.  There is a second table in the accdb with the sponsoring agencies and their unique ID #.  How do I display the actual agency name, and not the ID #, in a combobox?

    What I want to see is this:

         John Doe     ABC Corporation  (John Doe in textbox, ABC in combo)

    The results I'm getting is this however:

         John Doe     10

    I'm using the code below to display the agency and name.  I know this is not the right code but don't know what other code to use.  Thanks for any help or advice.

     

     

    DirectCast(e.DataRepeaterItem.Controls("cboSponsoringAgency"), ComboBox).Text = currItem("SponsoringAgencyID").ToString  'want combobox to display SponsoringAgency here - not ID

     

     

    DirectCast(e.DataRepeaterItem.Controls("txtName"), TextBox).Text = currItem("PersonName").ToString

    • Changed type db_dweeb Thursday, June 24, 2010 9:01 PM suggested
    Thursday, June 24, 2010 6:38 PM

Answers

  • Hi kris_hood,

    Are column "FacultyID" and "SponsoringAgency" belongs to the same table ? What is "currItem" ? In what event handler do you fill the textboxes and combobox ? Please refer to the following code which works for me:

    Private ds As DataSet
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      Dim strConn As String = "Data Source = .\SQLExpress; Initial Catalog = Northwind;Integrated Security = True"
      Dim strSql As String = "Select OrderID, CustomerID, OrderDate From Orders"
      ds = New DataSet
      Dim da As SqlDataAdapter = New SqlDataAdapter(strSql, strConn)
    
      da.Fill(ds, "Orders")
      Me.DataRepeater1.VirtualMode = True
      Me.DataRepeater1.DataSource = ds.Tables("Orders")
    End Sub
    
    Private Sub DataRepeater1_DrawItem(ByVal sender As System.Object, ByVal e As Microsoft.VisualBasic.PowerPacks.DataRepeaterItemEventArgs) Handles DataRepeater1.DrawItem
      If e.DataRepeaterItem.ItemIndex < ds.Tables("Orders").Rows.Count Then
        Dim txtBox As TextBox = DirectCast(e.DataRepeaterItem.Controls("TextBox1"), TextBox)
        txtBox.Text = ds.Tables("Orders").Rows(e.DataRepeaterItem.ItemIndex)("CustomerID")
    
        Dim com As ComboBox = DirectCast(e.DataRepeaterItem.Controls("ComboBox1"), ComboBox)
        Dim dv As DataView = New DataView(ds.Tables("Orders"))
        dv.RowFilter = "CustomerID = '" & ds.Tables("Orders").Rows(e.DataRepeaterItem.ItemIndex)("CustomerID").ToString & "'"
    
        com.DataSource = dv
        com.DisplayMember = "OrderDate"
        com.ValueMember = "OrderID"
      End If
    End Sub
    

    Best regards,
    Alex Liang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by db_dweeb Tuesday, June 29, 2010 1:57 PM
    Tuesday, June 29, 2010 8:29 AM
    Moderator

All replies

  •  

    ' First set the DataSource property of your combobox to the appropriate table
    
    myComboBox.DataSource = myTable
    
    'Next, decide what you want to see in the Combobox using the Displaymember property (the value that is displayed in the combobox:
    
    myComboBox.DisplayMember = AgencyName 'use the actual name of your column
    
    'Lastly, set the ValueMember property (ie, the value that gets written back to the database, usually the PK:
    
    myComboBox.ValueMember = AgencyNo 'or whatever

     

    First, change the TYPE of your question from General Comment to Question so that Forum members know that you're looking for an answer. Second, try clarifying your question particularly your table structure.

    Here's an idea based on my little understanding your issue:

     


    Only performance counts!
    Thursday, June 24, 2010 8:49 PM
  • Sorry, I'm not asking my question clearly enough.  I'm trying to display data from Access on a form, in a datarepeater control.  There are two controls in the datarepeater - a combobox and a textbox.  The textbox displays the name of the person, the combobox displays what agency they're in.  A person can be in more than one agency.

    The combobox datasource is set to display the actual agency name but store the agency id back to the access table.  However, when the data from the database is displayed on the form, the ID # displays, not the agency name.

    I want to see the datarepeater display this:

         John Doe (in the textbox)     ABC Corporation (in the combobox)

    But what I'm actually seeing is this:

         John Doe     10 (this is the ID # for ABC Corporation)

    (FWIW, this is an unbound datarepeater.)  I use the DirectCast method to fill both the textbox and the combobox with data.  The textbox works fine.  But I can't figure out how to tell the combobox to display ABC Corporation and not 10. 

    The only way I can figure out how to do this is by changing the datasource for the datarepeater to include the agency name.  I was wondering if there was another way to accomplish this.

    Friday, June 25, 2010 1:54 PM
  • Hi kris_hood,

    Could you please provide some sample code about how you fill both the textbox and the combobox ? Thanks.

    Best regards,
    Alex Liang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, June 28, 2010 7:54 AM
    Moderator
  • Here's how I fill the combo.  (This is working perfectly)

      Private Sub LoadSponsoringAgencyCombo()
    
        Dim daSponsoringAgencyCombo = New OleDbDataAdapter( _
     "SELECT * FROM tlkpSponsoringAgency ORDER BY SponsoringAgency", strConnectionString)
    
        'set up Credit Type combo box
        daSponsoringAgencyCombo.Fill(dsSponsoringAgencyCombo, "tlkpSponsoringAgency")
    
        Me.cboSponsoringAgency.DataSource = dsSponsoringAgencyCombo.Tables("tlkpSponsoringAgency")
        Me.cboSponsoringAgency.DisplayMember = "SponsoringAgency"
        Me.cboSponsoringAgency.ValueMember = "SponsoringAgencyID"
        Me.cboSponsoringAgency.Text = ""
    
      End Sub
    
    This is how I fill the textboxes:
     DirectCast(e.DataRepeaterItem.Controls("txtFacultyID"), TextBox).Text = currItem("FacultyID").ToString
     DirectCast(e.DataRepeaterItem.Controls("cboSponsoringAgency"), ComboBox).Text = currItem("SponsoringAgency").ToString
    
    txtFacultyID fills perfectly.  cboSponsoringAgency fills with the ID # instead of the agency name.  This is the code I cannot figure out how to make work the way I want.
    Monday, June 28, 2010 7:16 PM
  • Hi kris_hood,

    Are column "FacultyID" and "SponsoringAgency" belongs to the same table ? What is "currItem" ? In what event handler do you fill the textboxes and combobox ? Please refer to the following code which works for me:

    Private ds As DataSet
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      Dim strConn As String = "Data Source = .\SQLExpress; Initial Catalog = Northwind;Integrated Security = True"
      Dim strSql As String = "Select OrderID, CustomerID, OrderDate From Orders"
      ds = New DataSet
      Dim da As SqlDataAdapter = New SqlDataAdapter(strSql, strConn)
    
      da.Fill(ds, "Orders")
      Me.DataRepeater1.VirtualMode = True
      Me.DataRepeater1.DataSource = ds.Tables("Orders")
    End Sub
    
    Private Sub DataRepeater1_DrawItem(ByVal sender As System.Object, ByVal e As Microsoft.VisualBasic.PowerPacks.DataRepeaterItemEventArgs) Handles DataRepeater1.DrawItem
      If e.DataRepeaterItem.ItemIndex < ds.Tables("Orders").Rows.Count Then
        Dim txtBox As TextBox = DirectCast(e.DataRepeaterItem.Controls("TextBox1"), TextBox)
        txtBox.Text = ds.Tables("Orders").Rows(e.DataRepeaterItem.ItemIndex)("CustomerID")
    
        Dim com As ComboBox = DirectCast(e.DataRepeaterItem.Controls("ComboBox1"), ComboBox)
        Dim dv As DataView = New DataView(ds.Tables("Orders"))
        dv.RowFilter = "CustomerID = '" & ds.Tables("Orders").Rows(e.DataRepeaterItem.ItemIndex)("CustomerID").ToString & "'"
    
        com.DataSource = dv
        com.DisplayMember = "OrderDate"
        com.ValueMember = "OrderID"
      End If
    End Sub
    

    Best regards,
    Alex Liang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by db_dweeb Tuesday, June 29, 2010 1:57 PM
    Tuesday, June 29, 2010 8:29 AM
    Moderator
  • Thanks for your reply.  No, FacultyID and SponsoringAgency are not in the same table.  As far as currItem, it's some coding that I got from someone who was helping me set up the datarepeater.  Here's how it's declared:

        Dim currItem As DataRowView = bs.Item(e.DataRepeaterItem.ItemIndex)
    

    The more I work with vb.net, the more frustrated I become.  I guess I'm too used to the rapid database development that VBA provides and I get frustrated when I find it's not that rapid in vb.net.  It seems like there so much code that goes into vb.net for the simplest task.  I go down one path that worked well in one situation only to find that the same path does not work well with a slight variation in another situation.  There's a lot of opinions out there on what path to take which is very frustrating for a beginner.

    Thanks again.

    Tuesday, June 29, 2010 1:57 PM