Programatically adding items to a combobox ado.net

HeavenCore

Well-known member
Joined
Apr 10, 2007
Messages
77
Location
Bolton, England
Programming Experience
1-3
Programmatically adding items to a combobox ado.net

Hi i have a table with an id and a name field (amongst others). i have the following ado.net code populating a combobox:

VB.NET:
[SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] connectionstring [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]My[/COLOR][/SIZE][SIZE=2].Settings.CoreV1ConnectionString[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] theDatabase [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlClient.SqlConnection(connectionstring)[/SIZE]
[SIZE=2]theDatabase.Open()[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] SQLstatement [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlClient.SqlCommand([/SIZE][SIZE=2][COLOR=#800000]"SELECT HireSupplierID FROM tblHireSupplier"[/COLOR][/SIZE][SIZE=2], theDatabase)[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] QueryResults [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] SqlClient.SqlDataReader = SQLstatement.ExecuteReader()[/SIZE]
[SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE][SIZE=2] QueryResults[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] .HasRows [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]cboSelectSupplier.Items.Clear()[/SIZE]
[SIZE=2][COLOR=#0000ff]While[/COLOR][/SIZE][SIZE=2] .Read[/SIZE]
[SIZE=2]cboSelectSupplier.Items.Add(QueryResults(0).ToString)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]While[/COLOR][/SIZE]
[SIZE=2]cboSelectSupplier.Text = [/SIZE][SIZE=2][COLOR=#800000]"Please choose..."[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2].Close()[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE]
[SIZE=2]theDatabase.Close()[/SIZE]
[SIZE=2]theDatabase.Dispose()[/SIZE]
[SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] exc [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception[/SIZE]
[SIZE=2]MessageBox.Show([/SIZE][SIZE=2][COLOR=#800000]"Error Loading Hire Suppliers table Table"[/COLOR][/SIZE][SIZE=2], ApplicationName)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]

this obviously just gives me a list of useless numbers, now with normal databinding you can set display and value member separately, how can i do this programmatically?

(sorry about the indenting, things went fubar when i pasted code in)
 
Combo.DataSource = datatable
Combo.DisplayMember = "DISPLAY MEMBER"
Combo.ValueMember = "VALUE MEMBER"

If you need to know more info, look in the FormXXX.Designer.VB file..
The code will be there - the visual designer isnt magic or rocket science, it jsut writes code for you. You can write the same code with yur fingers for the same result
 
Back
Top