|
-
Feb 5th, 2010, 09:10 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Refresh contents of databound combobox
I have a combo box that's filled from a query in the database. If I add a field to the database, the new field won't show up in the form until I close it and reopen it.
So, how do I refresh the contents of that combo box?
I bound the data onto the combo box like this:
Code:
dbConn.Open()
dbAdpBook = New SqlClient.SqlDataAdapter("SELECT * FROM Publisher", dbConn)
dbDsetBook = New DataSet
dbAdpBook.Fill(dbDsetBook, "Publisher")
dbConn.Close()
If dbDsetBook.Tables("Publisher").Rows.Count > 0 Then
With cmbAuthor
.DisplayMember = "PublisherName"
.ValueMember = "PublisherID"
.DataSource = dbDsetBook.Tables("Publisher")
End With
End If
I tried copying the same code to the cmbAuthor's Click event, and it gave me this error:
Cannot bind to the new value member. Parameter name: value
and points to the .ValueMember = "PublisherID" line of code.
====================
ほんとにどもありがとう!
Rie Ishida
-
Feb 5th, 2010, 09:34 AM
#2
Re: Refresh contents of databound combobox
When you insert a new record to your database, instead of inserting it directly, you can add a new datarow to the datatable and then update that datatable to your database. If you choose to go this route, there's no need to "refresh" the combobox's datasource. However, if you choose to leave it the way it is now, you will need to re-query your database. You don't ned to rebind the datatable to your combobox though. Some thing like this will do:
Code:
Private Sub UpdatecmbAuthorDataSource()
Dim table As DataTable = DirectCast(cmbAuthor.DataSource, DataTable)
Using dbAdpBook As New SqlClient.SqlDataAdapter("SELECT * FROM Publisher", dbConn)
dbAdpBook.Fill(table)
End Using
End Sub
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Feb 5th, 2010, 09:42 AM
#3
Thread Starter
Addicted Member
Re: Refresh contents of databound combobox
 Originally Posted by stanav
When you insert a new record to your database, instead of inserting it directly, you can add a new datarow to the datatable and then update that datatable to your database. If you choose to go this route, there's no need to "refresh" the combobox's datasource. However, if you choose to leave it the way it is now, you will need to re-query your database. You don't ned to rebind the datatable to your combobox though. Some thing like this will do:
Code:
Private Sub UpdatecmbAuthorDataSource()
Dim table As DataTable = DirectCast(cmbAuthor.DataSource, DataTable)
Using dbAdpBook As New SqlClient.SqlDataAdapter("SELECT * FROM Publisher", dbConn)
dbAdpBook.Fill(table)
End Using
End Sub
The code above doesn't seem to refresh the combo box's contents; more like it appends the contents of the new query statement.
====================
ほんとにどもありがとう!
Rie Ishida
-
Feb 5th, 2010, 10:20 AM
#4
Re: Refresh contents of databound combobox
Simply clear the table before filling it...
Code:
table.Clear()
dbAdpBook.Fill(table)
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
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
|