• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 581
  • Last Modified:

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
0
Nerdy_Girl88
Asked:
Nerdy_Girl88
1 Solution
 
RanjeetRainCommented:
Did you try

combobox.Refresh

???
0
 
RanjeetRainCommented:
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?
0
 
Nerdy_Girl88Author Commented:
it is a databound combobox,  bound by a dataview from the data tier,  using vb.net
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Nerdy_Girl88Author Commented:
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?



0
 
Nerdy_Girl88Author Commented:
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.
0
 
Statick001Commented:
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
0
 
santosh26676Commented:
Refresh the DataView and then refresh the combobox.
0
 
Nerdy_Girl88Author Commented:
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.




0
 
DrewKCommented:
you simply need to REPAINT the form:

if your form is named "frmTest", your code would be:

frmTest.Repaint

or if you are calling it from another form, try: Form_frmTest!Repaint
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now