?
Solved

Trouble with Binding Source

Posted on 2014-12-28
3
Medium Priority
?
106 Views
Last Modified: 2014-12-29
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
0
Comment
Question by:randys106
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 34

Accepted Solution

by:
Mike Eghtebas earned 2000 total points
ID: 40521544
I just tested the following code and it works as described below. Please apply the changes noted below to work in your project:
  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        For Each r In WSCGSoftwareDataSet.Tables(0).Rows
            ListBox1.Items.Add(r(1))
        Next

    End Sub

Open in new window


WSCGSoftwareDataSet used here is my dataset name which is the same as WSCGSoftwareDataSet.xsd file in object browser, change it to whatever you have.

r(1) is the column index in your table. For now proceed with a single column (ex. firstname). But then use:

ListBox1.Items.Add(r(2) & ", " & r(1))

if Last Name has index 2 and First Name has index 1.

I have Button1_Click to run this code. You can put in form load if you wish.
0
 

Author Closing Comment

by:randys106
ID: 40521793
Perfect solution.  Thanks so much.
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40523057
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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

777 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