Results 1 to 12 of 12

Thread: [RESOLVED] How to fill a combo box at Run time

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2012
    Posts
    24

    Resolved [RESOLVED] How to fill a combo box at Run time

    How do I fill a combo box at run time and set the selected value as well? I'm filling the the combo box from a database.

  2. #2
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: How to fill a combo box at Run time

    please show us how your are fetching Records from database..
    __________________
    Rate the posts that helped you

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to fill a combo box at Run time

    Populate a DataTable and assign it to the DataSource property. Set the DisplayMember and ValueMember properties to the names of the column you want to display and the primary key column respectively. To select an item you would most likely either set the SelectedIndex to the index of the row you want to select or else set the SelectedValue to the ID of the row you want to select.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2012
    Posts
    24

    Re: How to fill a combo box at Run time

    The code is below. I want to put the year in the combo box for the user to select and then the database name to go in the selected value position.


    Code:
      Private Sub load_year_combobox()
            Try : Dim SQLdtr2 As Data.OleDb.OleDbDataReader : Dim SQLcmd As Data.OleDb.OleDbCommand
                Dim sql_select As String = "" : Dim timer_integer As Integer = 0 : Dim timer_count As Integer = 0
                sql_select = "SELECT db_name, year_id " + _
                    "FROM tbl_past ORDER BY ID"
                SQLcmd = New Data.OleDb.OleDbCommand(sql_select, mainconnection)
                mainconnection.Open() : SQLdtr2 = SQLcmd.ExecuteReader
                cmbYear.Items.Clear() : cmbYear.Items.Add("Select Year to Search")
                Do : While SQLdtr2.Read()
                        cmbYear.Items.Add(SQLdtr2(1))
                        'SQLdtr2(0) 
                    End While
                Loop While SQLdtr2.NextResult : mainconnection.Close()
            Catch ex As Exception
                MsgBox(ex.ToString) : m.errors(show_error, ex.ToString, error_l)
            End Try
        End Sub

  5. #5
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: How to fill a combo box at Run time

    as suggested by JMC you can fill the Datatable using OleDbDataAdapter and bind it with your comobox like:
    Code:
    cmbYear.DataSource = dtYear
    cmbYear.DisplayMemeber = "db_name"
    cmbYear.ValueMember = "year_id"
    __________________
    Rate the posts that helped you

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jan 2012
    Posts
    24

    Re: How to fill a combo box at Run time

    I would like to connect it with the way I posted earlier instead of connecting it to the data source. The reason is because I might read this data via a stream reader instead of the database. So how can I set the selected value with the code I posted?

  7. #7
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: How to fill a combo box at Run time

    Quote Originally Posted by tvb2727 View Post
    I would like to connect it with the way I posted earlier instead of connecting it to the data source. The reason is because I might read this data via a stream reader instead of the database. So how can I set the selected value with the code I posted?

    Not sure why cant you use DataTable even if you are fetching data from StreamReader?

    anyway the alternative could be:
    Code:
    Private Sub load_year_combobox()
            Try : Dim SQLdtr2 As Data.OleDb.OleDbDataReader : Dim SQLcmd As Data.OleDb.OleDbCommand
    Dim arrYear As New ArrayList
                Dim sql_select As String = "" : Dim timer_integer As Integer = 0 : Dim timer_count As Integer = 0
                sql_select = "SELECT db_name, year_id " + _
                    "FROM tbl_past ORDER BY ID"
                SQLcmd = New Data.OleDb.OleDbCommand(sql_select, mainconnection)
                mainconnection.Open() : SQLdtr2 = SQLcmd.ExecuteReader
                
                Do : While SQLdtr2.Read()
                        arrYear.Add(New ComboBoxItem(SQLdtr2 ("year_id"), SQLdtr2 "db_name"))               
     End While
                Loop While SQLdtr2.NextResult : mainconnection.Close()
    cmbYear.DataSource = arrYear
    cmbYear.DisplayMemeber = "db_name"
    cmbYear.ValueMember = "year_id"
            Catch ex As Exception
                MsgBox(ex.ToString) : m.errors(show_error, ex.ToString, error_l)
            End Try
        End Sub
    __________________
    Rate the posts that helped you

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to fill a combo box at Run time

    Quote Originally Posted by tvb2727 View Post
    I would like to connect it with the way I posted earlier instead of connecting it to the data source. The reason is because I might read this data via a stream reader instead of the database. So how can I set the selected value with the code I posted?
    That's not a valid reason. It doesn't matter where the data comes from. Just put it into a list - any list - and bind that list to the control as suggested. The list can be a DataTable, array, List(Of T) or any other object that implements the IList or IListSource interface.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jan 2012
    Posts
    24

    Re: How to fill a combo box at Run time

    This is what I want to do:

    Code:
        cmbYear.Items.Add(New comboboxitem(SQLdtr2(1), SQLdtr2(0)))
    but I have the error:

    Error 1 Type 'comboboxitem' is not defined.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jan 2012
    Posts
    24

    Re: How to fill a combo box at Run time

    Quote Originally Posted by jmcilhinney View Post
    That's not a valid reason. It doesn't matter where the data comes from. Just put it into a list - any list - and bind that list to the control as suggested. The list can be a DataTable, array, List(Of T) or any other object that implements the IList or IListSource interface.
    I've just never done much with data tables.

  11. #11
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: How to fill a combo box at Run time

    Forgot to add ComboboxItem Class
    Code:
    Public Class ComboBoxItem
    
    Private piyear_id As String
    
    Private pidb_name As String
    
    Public Sub New(ByVal stryearid As String, ByVal strdbname As String)
    
    piyear_id = stryearid 
    
    pidb_name = strdbName
    
    End Sub
    
    Public Property year_id() As String
    
    Get
    
    Return piyear_id 
    
    End Get
    
    Set(ByVal value As String)
    
    piyear_id = value
    
    End Set
    
    End Property
    
    Public Property db_name() As String
    
    Get
    
    Return pidb_name 
    
    End Get
    
    Set(ByVal value As String)
    
    pidb_name = value
    
    End Set
    
    End Property
    
     
    
    End Class
    __________________
    Rate the posts that helped you

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jan 2012
    Posts
    24

    Re: How to fill a combo box at Run time

    Thanks riteshjain1982. This is what I needed.

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