Link to home
Start Free TrialLog in
Avatar of Nerdy_Girl88
Nerdy_Girl88

asked on

How do i refresh a combobox?

Hi,

I have a form with a combobox, two text boxes and Add/Save/Edit and delete buttons.   When I dad a new record to the database I want to refresh the combobox.

Combobox.datasource = DvAccess(Dataview)

How do I refresh the combobox?  Do i need to reload it,  ie requery the database?  or just update the recordset?

lost and confused

thanks
Avatar of RanjeetRain
RanjeetRain

Did you try

combobox.Refresh

???
Nopes. Wait. Find out the name of the recorset it is bound to and refresh the bound recordset (call requery).

Is it a plain vanilla ComboBox or a Data bound Combo?
Avatar of Nerdy_Girl88

ASKER

it is a databound combobox,  bound by a dataview from the data tier,  using vb.net
thisfunction will fill the dataset that the dataview is based on.
-------------------------------------------------------
 Public Function getAccessType() As DataView
        'get the dataview of models for the combobox
        DacboAccess.Fill(DscboAccess1)
        Return Me.DvAccess
    End Function
--------------------------------------------------------
this method is called when the form loads, it will call for the dataset to be filled,  which will populate the combobox.  
--------------------------------------------------------
Private Sub LoadCombobox()
        Dim dvAccess As DataView
        Dim dsAccess As DataSet
        'use try catch to help performance
        Try
            'get the data tier and the dataview
            dvAccess = mobjData.getAccessType
            'bind the dataview to the combobox
            If mblnListInitialized = False Then
                With cboAccessSearch
                    .DataSource = dvAccess
                    .DisplayMember = "Access_Type"
                    .ValueMember = "Access_Type"
                    .SelectedIndex = -1
                End With
                mblnListInitialized = True
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
------------------------------------------------------

to refresh the dataset, i have tried to call the fill LoadCombobox() which i thought would fill the dataset again, with the new data added.

but i get this error on this line.
"DacboAccess.Fill(DscboAccess1)"

there is already a open datareader associated with this conectn which must be closed first.

Whats the dataerader. How can i refresh my dataset?



I have figured out that you can call the fill method many times. It will merge datarows that have the same prmary key and append new datarows to the dataset.  This will work for edit and add buttons, but it will not work for the delete button.  I want the deleted record to dissapear from the combobox.  Im trying to clear the dataset, before calling the dataAdapter fill method.  But i get this error!  

"object reference not set to an instance of an object."
------------------------------------------------------
Private Sub LoadCombobox()
        Dim dvAccess As DataView
        Dim dsAccess As DataSet
        'use try catch to help performance
        Try
            mobjData.DscboAccess1.Clear() //'this only gives the error when i delete, it performs the delete but with the error.
            dvAccess = mobjData.getAccessType
            'bind the dataview to the combobox
            If mblnListInitialized = False Then
                With cboAccessSearch
                    .DataSource = dvAccess
                    .DisplayMember = "Access_Type"
                    .ValueMember = "Access_Type"
                    .SelectedIndex = -1
                End With
                mblnListInitialized = True
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
---------------------------------------------------

thanks,  nearly solved this problem.
what i usually do is create a function which fills an arraylist with the relevant data, and binds the arraylist to the combobox. sorry but i dont use VB so i cant give example code, unless you are happy with c++ or c#

by creating the arraylist within the function, keeps it in local scope inside the function
so every time you call the function, its a new arraylist that is created, and so the combobox updates fine

the problem is, that if you try and bind the combobox to a datasource which it has already been bound to, it won't update. by keeping its datasource (in my example, an arraylist) to being something local to the function, it updates every time
Refresh the DataView and then refresh the combobox.
The dataview's datasource needs to be refreshed.  dataView DataSource is a dataset,  I have figured out that you can call the datasets fill method multiple times and it will up data this way, however it wll only update changes and new records that have been added to the database, it will not delete data that is in the recordset that isn't present in the database, i have tried to clear the dataset, but not luck.  I will try the refresh.




ASKER CERTIFIED SOLUTION
Avatar of DrewK
DrewK
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial