Link to home
Start Free TrialLog in
Avatar of randys106
randys106Flag for United States of America

asked on

Trouble with Binding Source

I have an application which accesses a database consisting of one table (Customers) with two fields (LastName and FirstName).  I drug the data source onto my form which automatically generated a data set, binding source, table adapter, table adapter manager, and a binding navigator. (I do not pretend to fully understand what each of these does).  This automatically generated text boxes for FirstName and LastName.

I want to load the data into an unbound listbox.

This code works:
    
        Me.CustomersBindingSource.MoveFirst()
        For i = 1 To CustomersBindingSource.Count
            ListBox1.Items.Add(Me.LastNameTextBox.Text + ", " + Me.FirstNameTextBox.Text)
            Me.CustomersBindingSource.MoveNext()
        Next

Open in new window


My listbox fills with last and first names.

But, if I change the visible property of the textboxes to False, the listbox fills with commas.  

I do not need the textboxes to be present except to make the above code work.  

Is there some way to get the data directly from the Binding Source (or Table Adapter or Binding Navigator, etc.)?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of randys106

ASKER

Perfect solution.  Thanks so much.
Hi Randy,

Also try this:

Adjust WSCGSoftwareDataSet, LastName, and ID to whatever you are having
       ListBox1.DataSource = WSCGSoftwareDataSet.Tables(0) 

        ListBox1.DisplayMember = "LastName"
        ListBox1.ValueMember = "ID"

Open in new window


The advantage of this will be use of:

 Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        If ListBox1.SelectedIndex > -1 Then
            MessageBox.Show(ListBox1.SelectedValue.ToString())
        End If
    End Sub

Open in new window


To give you the value of ID upon click on the list box.