Solved

How to bind data to comboxBox in vb.net

Posted on 2012-03-27
10
711 Views
Last Modified: 2012-08-13
Hi there,

How to bind data to combobox?

Thanks!!!
0
Comment
Question by:developer2012
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 125 total points
ID: 37774338
Try this

Dim dbadp As New SqlDataAdapter("Select ...", "connection string")
Dim dTable As New DataTable
dbadp.Fill(dTable)
dbadp.Dispose()
Combobox1.DisplayMember="columnname"
Combobox1.ValueMember="columnname"
Combobox1.DataSource = dTable
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 125 total points
ID: 37774351
What is the source of the data? For example, you could bind to a List:

Dim source As New List(Of String)

source.Add("red")
source.Add("orange")
source.Add("yellow")
source.Add("green")
source.Add("blue")
source.Add("indigo")
source.Add("violet")

Me.ComboBox1.DataSource = source

Open in new window


...or a DataTable:

Dim source As New DataTable

source.Columns.Add("color", GetType(String))

source.Rows.Add("red")
source.Rows.Add("orange")
source.Rows.Add("yellow")
source.Rows.Add("green")
source.Rows.Add("blue")
source.Rows.Add("indigo")
source.Rows.Add("violet")

Me.ComboBox1.DataSource = source
Me.ComboBox1.DisplayMember = "color"

Open in new window


...or any number of other sources. It all depends on what you are binding to.
0
 

Author Comment

by:developer2012
ID: 37774564
Public Class CrossingReport
    Private Sub BindComboBox()
     

  Dim conn As New OleDbConnection("Provider=SQLOLEDB;Data Source=xxx-xxx;User ID=tmds;Initial Catalog=tmdsDatabaseStatic")
        Dim command As String = "select DOT from dbo.tblSystemInformationHighwayCrossings"
        Dim cmd As New OleDbCommand(command, conn)
        Dim da As New OleDbDataAdapter(cmd)
        conn.Open()
        Dim table As New DataTable("dbo.tblSystemInformationHighwayCrossings")
        da.Fill(table)
        cbxDot.DisplayMemberPath = "DOT"
        cbxDot.DataContext = table
  End Sub

I used the above  code. But it does not do anything. AM I missing anything?
I am developing my application in "WPF".
Thanks
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 125 total points
ID: 37774578
If the source is an array, a collection or any class that his described as implementing the IList interface in the documentation, you can do the following. I am assuming that variable myCustomers is a collection of customers with an ID property that holds the customer number and Name that holds the user name.

ComboBox1.DataSource = myCustomers
ComboBox1.DisplayMember = "Name"
ComboBox1.ValueMember = "ID"

This will display the name of the customers, but will enable you to easily retrieve their ID when the user makes a selection. If you are working from a DataTable, specify the name of the field instead of the property.

When the user makes a selection, you can retrieve different values with code similar to the following:

customerID = CInt(ComboBox1.SelectedValue)
customerName = ComboBox1.Text

You can also retrieve the whole information about the customer through the SelectedItem property with code like the following:

Dim selectedCustomer as Customer
selectedCustomer = DirectCast(ComboBox1.SelectedItem, Customer)
0
 
LVL 5

Assisted Solution

by:Aruiz04
Aruiz04 earned 125 total points
ID: 37774755
instead of DataContext use itemsSource
and instead of binding to the datatabel bind to the defaultview of the datatable

cbxDot.itemsSource =  table.DefaultView;
cbxDot.DisplayMemberPath = "DOT";
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:developer2012
ID: 37775301
'Dim da As New OleDbDataAdapter(cmd)
        Dim ConnectionStringDummy As New OleDbConnection("Provider=SQLOLEDB;Data Source=xxxxxxx;UserID=xxxx;InitialCatalog=tmdsDatabaseStatic")
        Dim OleDbCommand1 As OleDbCommand
        Dim OleDbDataReader1 As OleDbDataReader

        OleDbCommand1 = New OleDbCommand("Select DOT from dbo.tblSystemInformationHighwayCrossings", ConnectionStringDummy)
        'ConnectionStringDummy.Open()
        ConnectionStringDummy.Open()
        OleDbDataReader1 = OleDbCommand1.ExecuteReader()
   
        While (OleDbDataReader1.Read())

            ComboBox1.Items.Add(OleDbDataReader1(0).ToString)
        End While

Okay, I implemented this code. Though my connections are open but my code does not seems to work?

Is there any thing I am missing  or

can somebody please explain me step by step how to bind a combobox with datatable and dispaly it?

@ Code Cruiser:  I applied the code which you gave me . IT does not show any error but never shows any output. :(.

@ Aruiz04:  I did your code but i keep getting an error message "ITemSource" is not a property of system,windows.controls.

Thanks Kaufmed. You idea gave me an insight into the problem.
0
 
LVL 40
ID: 37775390
A DataTable is a collection of rows. The technique I described for a collection works with DataTables.

Set the DataTable as the DataSource, and use the DisplayMember property to specify which field you want to be listed.
0
 

Author Comment

by:developer2012
ID: 37775524
@ JamesBurger: Thanks. Code in vb.net works perfectly fine. But, When I use the same techniques in wpf. It does not display any data in the comboBox. Thanks
0
 

Author Comment

by:developer2012
ID: 37775538
Okay Thanks All. Just dropped the bugger right into the ComboBox and does displays data now :)
0
 
LVL 5

Expert Comment

by:Aruiz04
ID: 37777392
The code I gave you is for WPF controls, that's what you said earlier

-> I am developing my application in "WPF".
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now