Solved

Trouble with Binding Source

Posted on 2014-12-28
3
101 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
  • 2
3 Comments
 
LVL 33

Accepted Solution

by:
Mike Eghtebas earned 500 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 33

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

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