Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Basic Combo box functionality

Status
Not open for further replies.

696796

Programmer
Aug 3, 2004
218
GB
Hi all,

New to vb.net and the use of datasets...

I know this topic has been covered numerous times, but every Q doesn't start from the begining.

I simply have a form with a combobox on (cboSupplier) I want to fill it with supplier name from my Access table (tblSupplier) taking the supplierID and supplierName....

Now i get confused with knowing what to use in terms of a dataset. What on earth is the easiest function to use? I am looking at oleDB stuff and getting confused. How do i firstly create a dataset, then apply it to my combo???

Many thanks guys and gals'

Alex
 
What code do you have so far? Have you tried finding out how to return a dataset before you try to do data binding? The peeps here are great to help, but you typically should try to get through the code as much as possible before asking for help.

If you need search terms for google, use DATASET and DATAADAPTER. Tack Visual Basic on to that and you should get quite a few easy tutorials.
 
Thanks for the reply...

I have this in my gui with the comboboxes..

Code:
    Private Sub frmCar_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim populating As dbaCAR = New dbaCAR
        populating.PopulateCombos("tblSupplier", Me.cboSupplier)
        populating.PopulateCombos("tblDefect", Me.cboDefect)

    End Sub

and this in a global database class

Code:
    Public Sub PopulateCombos(ByVal strTable As String, ByVal cmbControl As ComboBox)
        ds = New DataSet
        da = New OleDbDataAdapter("SELECT * FROM " + strTable, conn)
        da.Fill(ds, strTable)
        cmbControl.DataSource = ds.Tables(0)
        cmbControl.ValueMember = ds.Tables(0).Columns(0).ToString
        cmbControl.DisplayMember = ds.Tables(0).Columns(1).ToString
    End Sub

conn is the connection string

Al
 
Hello!

Your code looks to be working, BUT your combobox will NEVER get filled. Heheh
If you wonder.. why : ByVal cmbControl As ComboBox

Using ByVal, then all the changes are applied LOCALY (at the 'cmbControl' which's scope is private and only visible in the 'PopulateCombos' sub). You must use ByRef !

:)
 
God damn it - it does work - i had the wrong path to a different database with the same tablename, so i was getting random output. Works now cheers...

While i'm here, if i want to 'hard code' in two values for a combobox - ie, the combo will pull down and display 'open' and 'closed' - these will only ever be the two options so no need for a table. How do i do this??
 
make a arraystring and bind that.

or

use

combo1.items.add("blah")

or

something else

Please do not use the first two words in that order.

Christiaan Baes
Belgium

"My new site" - Me
 
Cheers pal,

This is how i display my combo (properties)
Code:
cmbControl.DataSource = ds.Tables(0)
cmbControl.ValueMember = ds.Tables(0).Columns(0).ToString
cmbControl.DisplayMember = ds.Tables(0).Columns(1).ToString

Doing this there the first record is shown, can i make it blank to start with?
 
You can try cmbControl.selectedindex=-1 BUT i don't think it will work... And that's because it is bound to a datasource.
 
Actually it worked like a charm - many thanks
 
You can also add a blank data row to your dataset at position 0 to get that effect.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top