Results 1 to 4 of 4

Thread: [RESOLVED] Refresh contents of databound combobox

  1. #1

    Thread Starter
    Addicted Member riechan's Avatar
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    254

    Resolved [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

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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 -

  3. #3

    Thread Starter
    Addicted Member riechan's Avatar
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    254

    Re: Refresh contents of databound combobox

    Quote Originally Posted by stanav View Post
    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

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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
  •  



Click Here to Expand Forum to Full Width