Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to bind data to comboxBox in vb.net

Posted on 2012-03-27
10
Medium Priority
?
757 Views
Last Modified: 2012-08-13
Hi there,

How to bind data to combobox?

Thanks!!!
0
Comment
Question by:developer2012
[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
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 375 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 375 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 375 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 375 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
 

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

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

618 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