?
Solved

How to bind data to comboxBox in vb.net

Posted on 2012-03-27
10
Medium Priority
?
762 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 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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 long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

839 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