|
-
Jan 15th, 2012, 11:04 PM
#1
Thread Starter
Junior Member
[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.
-
Jan 15th, 2012, 11:07 PM
#2
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 
-
Jan 15th, 2012, 11:13 PM
#3
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.
-
Jan 15th, 2012, 11:21 PM
#4
Thread Starter
Junior Member
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
-
Jan 15th, 2012, 11:29 PM
#5
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 
-
Jan 15th, 2012, 11:33 PM
#6
Thread Starter
Junior Member
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?
-
Jan 15th, 2012, 11:46 PM
#7
Re: How to fill a combo box at Run time
 Originally Posted by tvb2727
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 
-
Jan 15th, 2012, 11:47 PM
#8
Re: How to fill a combo box at Run time
 Originally Posted by tvb2727
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.
-
Jan 15th, 2012, 11:51 PM
#9
Thread Starter
Junior Member
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.
-
Jan 15th, 2012, 11:52 PM
#10
Thread Starter
Junior Member
Re: How to fill a combo box at Run time
 Originally Posted by jmcilhinney
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.
-
Jan 15th, 2012, 11:54 PM
#11
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 
-
Jan 16th, 2012, 12:28 AM
#12
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|