?
Solved

Combobox not showing displaymember

Posted on 2012-08-17
17
Medium Priority
?
2,566 Views
Last Modified: 2012-08-18
I'm using VB 2010

My issue is I have done "use data bound items" on a combobox and have selected the parent table as the datasource. Then I set the display member to the firstname I want shown and the value member to the techId field (primarykey). Finally I set the selected value to the foreign key on the table that I'm working on, which it uses the same name and data type (techId / int). Below inc = -1 gets loaded when the form opens so when I hit the next button it just goes through the rows by adding 1.
cbTechId.DisplayMember = cbClientId.ValueMember(ds.Tables("Firstdb").Rows(inc).Item("TechId"))
txtNotes.Text = ds.Tables("Firstdb").Rows(inc).Item("Notes")

Open in new window

The combobox cbTechId does not show anything when I'm navigating through the dataset, but all the other fields that I've set not using comboboxes are like txtNotes, which is a textbox.

I'm not getting any errors.

I believe since I'm getting data for the rest of the code and everything else is working it either has to do with...
1) The way I bound the combobox or 2) The way I coded how to use the combobox
0
Comment
Question by:easyworks
  • 8
  • 6
  • 2
  • +1
17 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38304967
I am confused with your question. Are you populating the combobox and its not showing values or are you setting the selected value? If later then try

cbClientId.SelectedValue=ds.Tables("Firstdb").Rows(inc).Item("TechId")
0
 
LVL 1

Author Comment

by:easyworks
ID: 38304980
I just realized I messed up copying and pasting...
cbTechId.DisplayMember = cbTechId.ValueMember(ds.Tables("Firstdb").Rows(inc).Item("TechId"))

Open in new window

I am populating the combobox so when i navigate through the dataset it will display the techsname.
0
 
LVL 35

Expert Comment

by:YZlat
ID: 38305011
Where are you setting DataSource property of your combo box??
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 35

Expert Comment

by:YZlat
ID: 38305016
cbTechId.DataSource=ds.Tables("Firstdb")
cbTechId.DisplayMember = "TechId"
txtNotes.Text = ds.Tables("Firstdb").Rows(inc).Item("Notes") 

Open in new window

0
 
LVL 35

Expert Comment

by:YZlat
ID: 38305029
or

With cbTechId       
        .DataSource = ds.Tables("Firstdb")
        .DisplayMember = "TechId"
        .ValueMember = "TechId"
        .SelectedIndex = 0
    End With
txtNotes.Text = ds.Tables("Firstdb").Rows(inc).Item("Notes")  

Open in new window

0
 
LVL 1

Author Comment

by:easyworks
ID: 38305035
Yzlat are you saying I should uncheck the box for "use data bound items" and populate the combobox myself?
0
 
LVL 35

Expert Comment

by:YZlat
ID: 38305237
if you don't want to do that, then why are you setting DisplayMember programmatically?

cbTechId.DisplayMember = cbClientId.ValueMember(ds.Tables("Firstdb").Rows(inc).Item("TechId"))
txtNotes.Text = ds.Tables("Firstdb").Rows(inc).Item("Notes")
0
 
LVL 1

Author Comment

by:easyworks
ID: 38305289
I'll already have a dataset I created by dropping the tables from the database into a dataset.xsd in my program. I though I could just bind this control to it and make it work? What I'm doing is not working maybe I can not go about doing it this way I don't know, but I'm really confused because I do not see how when I bind doing what you are saying that the combobox will automaticaly change as I navigate to the next record.
cbTechId.DisplayMember = cbTechId.ValueMember(ds.Tables("Firstdb").Rows(inc).Item("TechId)

Open in new window

So do I just add your code above what I'm doing?
0
 
LVL 35

Expert Comment

by:YZlat
ID: 38305410
If your combobox is bound to datatable ds.tables("Firstdb"), youcannot set its DisplayMember to a valuemember of a different combobox.

You will need to provide more information on what are you doing here and post more code
0
 
LVL 1

Author Comment

by:easyworks
ID: 38305477
I messed up when I was copying and pasting earlier it was not supposed to be for a different combobox.
 cbTechId.DisplayMember = cbTechId.ValueMember(ds.Tables("Firstdb").Rows(inc).Item("TechId)
0
 
LVL 35

Expert Comment

by:YZlat
ID: 38305555
I believe

cbTechId.DisplayMember = cbTechId.ValueMember(ds.Tables("Firstdb").Rows(inc).Item("TechId)

should be

cbTechId.DisplayMember = "TechId"
0
 
LVL 1

Author Comment

by:easyworks
ID: 38305715
Here is some more code, because I dont understand. The problem is displaying the techsname while I navigate the records.

 Public Function connection()

        con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\SkyDrive\VB\Projects\MyFirstDB\MyFirstDB\firstdb.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"

        Return con
    End Function

Open in new window


Imports System.Data.Linq
Imports System.Data.SqlClient
Public Class frmSecond

    Dim database As New linqfirstdbDataContext
    'connection() is grabbed from dbDataConnection to create a connection to SQL DB
    Dim con As SqlConnection = connection()
    'adapter for SQL to fill ???
    Dim da As SqlDataAdapter
    'new object dataset so we can grab the information from the database to store here?
    Dim ds As New DataSet
    'variable to say what the last row is.
    Dim MaxRows As Integer
    'Counts when navigating forwards and backwards for your database rows.
    Dim inc As Integer

    Private Sub frmSecond_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        'TODO: This line of code loads data into the 'FirstdbDataSet.Invoices' table. You can move, or remove it, as needed.
        Me.InvoicesTableAdapter.Fill(Me.FirstdbDataSet.Invoices)
        'TODO: This line of code loads data into the 'FirstdbDataSet.Techs' table. You can move, or remove it, as needed.
        Me.TechsTableAdapter.Fill(Me.FirstdbDataSet.Techs)
        'TODO: This line of code loads data into the 'FirstdbDataSet.Clients' table. You can move, or remove it, as needed.
        Me.ClientsTableAdapter.Fill(Me.FirstdbDataSet.Clients)
        'TODO: This line of code loads data into the 'FirstdbDataSet.Organizations' table. You can move, or remove it, as needed.
        Me.OrganizationsTableAdapter.Fill(Me.FirstdbDataSet.Organizations)

        con.Open()

        Dim query = (From invo In database.Invoices
                    Select invo).ToString

        da = New SqlDataAdapter(query, con)
        da.Fill(ds, "firstdb")
        con.Close()

        MaxRows = ds.Tables("Firstdb").Rows.Count
        inc = -1

        For Each ctl As Control In Me.Controls
            If TypeOf ctl Is ComboBox Then
                DirectCast(ctl, ComboBox).SelectedItem = Nothing
            End If
        Next ctl

        ButtonViews()

    End Sub

    Private Sub NavigateRecords()

        cbOrgId.DisplayMember = cbOrgId.ValueMember(ds.Tables("Firstdb").Rows(inc).Item("OrgId"))
        cbClientId.DisplayMember = cbClientId.ValueMember(ds.Tables("Firstdb").Rows(inc).Item("ClientId"))
        cbTechId.DisplayMember = cbTechId.ValueMember(ds.Tables("Firstdb").Rows(inc).Item("TechId"))
        dtpDate.Value = ds.Tables("Firstdb").Rows(inc).Item("theDate")
        txtNotes.Text = ds.Tables("Firstdb").Rows(inc).Item("Notes")
        txtParts.Text = ds.Tables("Firstdb").Rows(inc).Item("Parts")
        txtLabor.Text = ds.Tables("Firstdb").Rows(inc).Item("Labor")
        txtMileage.Text = ds.Tables("Firstdb").Rows(inc).Item("Mileage")
        txtTotalCost.Text = ds.Tables("Firstdb").Rows(inc).Item("TotalCost")
        txtInvoiceNumber.Text = ds.Tables("Firstdb").Rows(inc).Item("InvoiceNumber")

    End Sub
Private Sub btnPrevious_Click(sender As System.Object, e As System.EventArgs) Handles btnPrevious.Click
        If inc > 0 Then
            inc = inc - 1
            NavigateRecords()
        Else
            MsgBox("First Record")
        End If
    End Sub

    Private Sub btnNext_Click(sender As System.Object, e As System.EventArgs) Handles btnNext.Click
        If inc <> MaxRows - 1 Then
            inc = inc + 1
            NavigateRecords()
        Else
            MsgBox("No More Rows")
        End If
    End Sub

Open in new window

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38306364
As I said, you are using display member just to select a value which is wrong. DisplayMember is used to tell combobox which column in the datatable it should display. Is your problem loading the combobox or is your problem in selecting the correct value in a loaded combobox? If later, as I said, setting the SelectedValue should work.
0
 
LVL 1

Author Comment

by:easyworks
ID: 38306397
I'm trying to pull from the dataset the firstname of the tech. The table it pulls from is a int which is the Id. The Id goes to a parent table which is the PK that has a firstname of the tech.

Both
cbTechId.SelectedValue = cbTechId.ValueMember(ds.Tables("Firstdb").Rows(inc).Item("TechId"))

Open in new window

and this
cbTechId.SelectedValue = cbTechId.SelectedValue(ds.Tables("Firstdb").Rows(inc).Item("TechId"))

Open in new window

does not display anything. If I do not use (ds.Tables("Firstdb").Rows(inc).Item("TechId")) something similar to this that allows me to change the row it is looking at then I will not be able to use the navigational button.
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 2000 total points
ID: 38307151
You seem to completely misunderstand the use of the different properties of a data binded ComboBox.

The DisplayMember is simply the name of the field (column) that you want to show in the list. So if you want to display the FirstName field, you code should be:

cbTechId.DisplayMember = "FirstName" 'Or whatever the field is named.

The ValueMember is the name of the field (column) that you want to use in your code, most often the ID key:

cbTechId.ValueMember = "ID" 'Or whatever the field is named.

You do not set the SelectedValue. You use it to retrieve the ValueMember after the user has made a selection:

ID = CInt(cbTechID.SelectedValue)

Once you have set the DisplayMember and the ValueMember as described, the following should display the FirstName of the Tech whose ID is defined in the row pointed to by inc:

cbTechId.SelectedValue = ds.Tables("Firstdb").Rows(inc).Item("TechId")
0
 
LVL 1

Author Comment

by:easyworks
ID: 38307935
OOHHHHH. I thought when I did that all in the databind on the windows form portion I wouldn't have to specify it in the code. Thanks for being so patient with me.
0
 
LVL 1

Author Closing Comment

by:easyworks
ID: 38307936
Thanks again for your help and being taking time to explain it to me.
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Integration Management Part 2
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question