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.
Printable View
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.
please show us how your are fetching Records from database..
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.
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
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"
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
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.
This is what I want to do:
but I have the error:Code:cmbYear.Items.Add(New comboboxitem(SQLdtr2(1), SQLdtr2(0)))
Error 1 Type 'comboboxitem' is not defined.
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
Thanks riteshjain1982. This is what I needed.