Databinding in vb.net combo box in winforms

Databinding in vb.net combo box in winforms

Page 1 of 1

6 Replies - 20483 Views - Last Post: 21 December 2009 - 03:51 AM Rate Topic: -----

#1 nav234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 04-December 09

Databinding in vb.net combo box in winforms

Post icon  Posted 18 December 2009 - 10:28 PM

Hi

How to bind data from database to combobox of winform in VB.NET .

Database : MSSQL server managemant studio

Front End : Vb.net 2008

In the combo box binding,i need to put "Product_gid"
in index and "Product_Name" in the value of combo box.


ONE more thing ,

How do i add an item "Select" in the 0'th position of Combo box,
So that every time i opened the list of combo box,
the 1st item will be "Select",other from database.



Can anyone help on this issue ???

Thanks in advance

Regards

Naveen... B)

Is This A Good Question/Topic? 0
  • +

Replies To: Databinding in vb.net combo box in winforms

#2 billyboy630  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 94
  • Joined: 09-October 09

Re: Databinding in vb.net combo box in winforms

Posted 18 December 2009 - 11:47 PM

Combobox.displaymember = " Whatever datafield you want"
Combobox.datasource =" your datasource"
Combobox.valuemember = "Whatever you want your value member to be"

Comboxbox.selectedindex = whatever number you like

Does this help?
Was This Post Helpful? 2
  • +
  • -

#3 sharpy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 35
  • View blog
  • Posts: 394
  • Joined: 02-June 07

Re: Databinding in vb.net combo box in winforms

Posted 19 December 2009 - 01:31 AM

Hi

Assuming you have already added a data source to a dataset in your project you can do this.

1. Add a combobox to your form.
2, Select the small arrow at the top right hand corner.
3. Check the use data bound objects.
4. Select the data source which will be your table you want to reference in your dataset
5.Select the display member you want to show in your combobox.
6. Run your app

The data you have selected to show should now be in you combobox.

Hope this is what you are looking for.
Was This Post Helpful? 0
  • +
  • -

#4 karimi  Icon User is offline

  • Banned
  • member icon

Reputation: -9
  • View blog
  • Posts: 104
  • Joined: 29-May 09

Re: Databinding in vb.net combo box in winforms

Posted 19 December 2009 - 06:03 AM

To add last object as "Please Select"

Do the following in the Load Event handler of form

cbo1.items.add("Please Select")
Was This Post Helpful? 0
  • +
  • -

#5 nav234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 04-December 09

Re: Databinding in vb.net combo box in winforms

Posted 21 December 2009 - 12:22 AM

Hi

Thank u for ur response.

But after i did what u said.

Im getting an error while loading of this
Combo box

cboProductName.Items.Add("Please Select")

Error : "Items collection cannot be modified when the DataSource property is set."

So how to rectify this

Thanks in advance

Regards
Naveen...
Was This Post Helpful? 0
  • +
  • -

#6 Metitron  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 78
  • Joined: 13-October 09

Re: Databinding in vb.net combo box in winforms

Posted 21 December 2009 - 01:57 AM

First if you want it to have "Please select" as an item you have to unbind the data source and populate the items at run time using a dbReader. how ever if you just want the box to say "Please select" at run time you can code, in the load event, combobox1.text="Please select" this wont affect the items list but will inform the user of what they need to do. if you want to use the dbReader try this
Imports System.Data.OleDb 'have to have to use oledb commands
Public Class Form1

	Dim cn As OleDbConnection ' connets to the db
	Dim cmd As OleDbCommand
	Dim dr As OleDbDataReader ' the reader
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		cn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\(Your database name)") 'this is for access 2007
		cn.Open()
		cmd = New OleDbCommand("SELECT * From (your table name)", cn)
		dr = cmd.ExecuteReader
		While dr.Read()
			TextBox1.Text = dr(0) 'each of these reads one column so you only 
			TextBox2.Text = dr(1) ' need 0 to however many columns you have -1
			TextBox3.Text = dr(2)
			TextBox4.Text = dr(3)
			TextBox5.Text = dr(4)
			TextBox6.Text = dr(5)
			TextBox7.Text = dr(6)
			TextBox8.Text = dr(7)
			TextBox9.Text = dr(8)
			TextBox10.Text = dr(9)
			If VehicleYearComboBox.Items.Contains(dr(1)) Then
			Else
				VehicleYearComboBox.Items.Add(dr(1))
			End If
		End While
		dr.Close()
		cn.Close()
	End Sub
End Class

Hope this helps. it took me forever to find how to do this
Was This Post Helpful? 0
  • +
  • -

#7 nav234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 04-December 09

Re: Databinding in vb.net combo box in winforms

Posted 21 December 2009 - 03:51 AM

Hi

thanks for ur help and ur response man

But

Here iam using SQL server managemant studio and not MYsql,

So based on tat if u tell me,it would be more helpful.

Requirement is

After i clicked dropdown in combo box ,the list opens with items
from database(MSSQL) .
In that drop list,i want to show "Please Select" on top of list which is droped down

Thanks in advance

Regards

Naveen..... :rolleyes:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1