Update the ComboBox at runtime???  
Author Message
fiaolle





PostPosted: Visual Basic Language, Update the ComboBox at runtime??? Top

Hi

I have tried to update my combobox with no luck. I add records to the table,
the combobox has as Datasource, but I don't know how to update the combobox.
I have tried updata the OleDBAdapter and the DataSet and set the datasource
to that dataset, but nothing happens. I can see in the database that I've
added a new record to the table, but nothing happens to the combobox when I
update it.
Does anyone know how to solve this.

I also don't know how to clear the ComboBox, I know that when I wan't to clear the DataGrid I use the code SetDataBinding(Nothing,Nothing). How do I clear a ComboBox

Please Help me!

Fia



Visual Basic1  
 
 
Ayhan Yerli





PostPosted: Visual Basic Language, Update the ComboBox at runtime??? Top

I don't know if it's the best sollution for your application, but it works.. Call this sub after you update the database.

Private Sub ReFillCombo()

Me.ComboBox1.Items.Clear() ' Clear the items of the combobox

Dim constr As String = "Your Connection String"

Dim conn As New SqlConnection(constr)

Dim da As New SqlDataAdapter("Your select command", conn)

Dim dt As New DataTable

da.Fill(dt)

Me.ComboBox1.DisplayMember = "The field which you want to display"

Me.ComboBox1.ValueMember = "The value member (mostly used for ID fields)"

Me.ComboBox1.DataSource = dt

End Sub



 
 
spotty





PostPosted: Visual Basic Language, Update the ComboBox at runtime??? Top

You say you have added records to the table and the combobox has this as a datasource.

You have to be aware that there is a data structure called a datatable and there is a database table. These are associated with each other often but are distinctly different. The datatable is a local data type (just like a integer or string) which is populated normally as a result of a action resulting in retrieval of records from a database table. This creates a disconnected snapshot copy of the database table which can be used for actions such as data binding which is what you are doing when you are setting a datasource.

Apart from setting the datasource, you would need to set the datamember which is the field in the datatable you wish to use for displaying in the combobox.

That said - when you add records to the datatable - these will be reflected immediately in the combobox. But if you exit the application and start it up again these will not be there as you have not persisted them (saved) to the database table. To do this you would issue an update method call on the tableadapter which would update the database table records with those in the datatable. To initially populate the datatable (which is actually part of a dataset type) you would issue a fill method call on a table adapter which would create a datatable variable from a database table.

As these two are disconnected and you are binding to a datatable not directly with the database table. You need to ensure that if you are writing code that is updating the database table directly (as with Ado.net code) that you repopulate the local datatable afterwards to ensure that you get an updated snapshot of the database - which you would use for databinding on the combobox.

This may be achieved using a simple fill method call on a table adapter - or recalling whatever method you used to originally populate the datatable in the first place. As you arent showing any code - its difficult to see what you are doing but if your saying the database is getting updated but the combobox isnt then I would take a guess that you need to repopulate the dataset after you save the record to ensure that the combobox has the latest copy of the database table contents.