Solved

How to bind data to comboxBox in vb.net

Posted on 2012-03-27
10
705 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 74

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

11 Experts available now in Live!

Get 1:1 Help Now