Solved

How to bind data to comboxBox in vb.net

Posted on 2012-03-27
10
724 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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
 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Help 27 57
Complex SQL statement in VB.NET 7 31
Why do I get the message "Message has been thrown by target of an invocation"? 22 53
run a stored procedure from vb.net 1 27
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 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