Results 1 to 8 of 8

Thread: Populating a Combobox - What's the Best Way?

  1. #1

    Thread Starter
    Fanatic Member The_Grudge's Avatar
    Join Date
    Jan 2005
    Location
    Canada
    Posts
    836

    Populating a Combobox - What's the Best Way?

    Is there a preferred way to populate a combobox in VB.NET ?

    I'm relatively new to .NET (although fluent in VB 6.0), and I'm trying to write an add on to an existing program. The add on is simply to filter the items in a treeview control.

    For example, when the user selects "Johnny B. Goode" from the combobox, I want to see only Johnny's items in the treeview. There is an existing dataset in the application that queries the "user" table, so I'm assuming I could use that to populate the combobox which contains a list of users. I also see an option to create "SQL" in the control.

    What's the best way, or does it really matter?

    I'd like to get it setup so it puts the user name in the combobox and then the "ID" into the .tag property.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Populating a Combobox - What's the Best Way?

    what is the structure of the user table + how do you populate your treeview?
    what you're asking would be easily accomplished through querying the db.

  3. #3

    Thread Starter
    Fanatic Member The_Grudge's Avatar
    Join Date
    Jan 2005
    Location
    Canada
    Posts
    836

    Re: Populating a Combobox - What's the Best Way?

    Hi .paul.

    The structure of the user table is dead simple. It's just ID (e.g. 1,2,3,4), UserName (e.g. John Smith), and Active (e.g. "True" or "False"). I can't speak to the treeview population that well because I didn't write the app. It appears as though the original author loops through a "Dataset" to populate it. The app is absolutely packed with Datasets.

    All I need to do is query the user table to put the user name in the combobox and the ID in behind the scenes somewhere so I can write that value to another table where the other data is stored.

    I can easily "bind" the combobox to the user table by clicking the 'create SQL' tab within the control on the form, but there's an issue when I do that. I need to be able to add my own option for "All" in case the user doesn't want to filter the list by user.

    I could just do what I know from VB6 and run an SQL query and loop through populating the combobox, but I've had difficulty finding a decent example of that online.

    Being new to VB.NET I easily get caught up in terms like DataReader, TableAdapter, and more.
    Last edited by The_Grudge; Nov 28th, 2012 at 05:59 PM.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Populating a Combobox - What's the Best Way?

    here's how to populate your combobox from a db:

    Code:
    'this is for access
    Imports System.Data.OleDb
    'this is for sql
    Imports System.Data.SqlClient
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'to use this you'll need to set your target cpu to x86
            'change the filename in the connection string. if you're not using access you can get a suitable connection string at:
            'www.connectionstrings.com
    
            Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccess2007file.accdb;Persist Security Info=False;")
            Dim da As New OleDbDataAdapter("SELECT [ID], [UserName] FROM [user]", con)
    
            ''sqlclient version
            'Dim con As New SqlConnection("connection string")
            'Dim da As New SqlDataAdapter("SELECT [ID], [UserName] FROM [user]", con)
    
            Dim dt As New DataTable
            da.Fill(dt)
    
            'add an All option to datatable
            Dim dr As DataRow = dt.NewRow
            dr.ItemArray = New Object() {-1, "All"}
            dt.Rows.InsertAt(dr, 0)
    
            ComboBox1.DisplayMember = "UserName"
            ComboBox1.ValueMember = "ID"
            ComboBox1.DataSource = dt
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
            'this is the ID field
            MsgBox(ComboBox1.SelectedValue.ToString)
        End Sub
    
    End Class

  5. #5

    Thread Starter
    Fanatic Member The_Grudge's Avatar
    Join Date
    Jan 2005
    Location
    Canada
    Posts
    836

    Re: Populating a Combobox - What's the Best Way?

    Quote Originally Posted by .paul. View Post
    here's how to populate your combobox from a db:

    Code:
    'this is for access
    Imports System.Data.OleDb
    'this is for sql
    Imports System.Data.SqlClient
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'to use this you'll need to set your target cpu to x86
            'change the filename in the connection string. if you're not using access you can get a suitable connection string at:
            'www.connectionstrings.com
    
            Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccess2007file.accdb;Persist Security Info=False;")
            Dim da As New OleDbDataAdapter("SELECT [ID], [UserName] FROM [user]", con)
    
            ''sqlclient version
            'Dim con As New SqlConnection("connection string")
            'Dim da As New SqlDataAdapter("SELECT [ID], [UserName] FROM [user]", con)
    
            Dim dt As New DataTable
            da.Fill(dt)
    
            'add an All option to datatable
            Dim dr As DataRow = dt.NewRow
            dr.ItemArray = New Object() {-1, "All"}
            dt.Rows.InsertAt(dr, 0)
    
            ComboBox1.DisplayMember = "UserName"
            ComboBox1.ValueMember = "ID"
            ComboBox1.DataSource = dt
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
            'this is the ID field
            MsgBox(ComboBox1.SelectedValue.ToString)
        End Sub
    
    End Class
    Thanks. Ill try it tomorrow from work. One question. The previous developer has a connection setup in the Project-->properties. Is it a detrimental to performance if I create my own connection? If so, is there a way I can use his existing connection?

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,397

    Re: Populating a Combobox - What's the Best Way?

    You can certainly use the existing connection by doing something along these lines:

    Code:
    Dim con As OleDBConnectionString = My.Settings.ExistingConnectionString
    Many times the connection string gets saved in the settings because it was added via the wizard tool in visual studios.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Populating a Combobox - What's the Best Way?

    Quote Originally Posted by The_Grudge View Post
    The previous developer has a connection setup in the Project-->properties. Is it a detrimental to performance if I create my own connection? If so, is there a way I can use his existing connection?
    not sure about that. start a new thread for that question.

  8. #8

    Thread Starter
    Fanatic Member The_Grudge's Avatar
    Join Date
    Jan 2005
    Location
    Canada
    Posts
    836

    Re: Populating a Combobox - What's the Best Way?

    Quote Originally Posted by dday9 View Post
    You can certainly use the existing connection by doing something along these lines:

    Code:
    Dim con As OleDBConnectionString = My.Settings.ExistingConnectionString
    Many times the connection string gets saved in the settings because it was added via the wizard tool in visual studios.
    Thanks. Part of the problem w this app I think is that the whole thing was put together with wizards!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width