Link to home
Start Free TrialLog in
Avatar of ccoton
ccoton

asked on

Can't change the datasource of a ComboBox (winforms)

I have a combobox that I've successfully bound (the first time, at least) to a dataset.  When a button is clicked on the form, I grab the dataset, set the .DataSource, .DisplayMember and .ValueMember-  When I then make the combobox visible, everything works as advertised.  When a selection is made from the combo, I reset my .DataSource to nothing.  

Here's the problem-  The second time the user clicks the button to bring up the same combo, I confirmed that the dataset is successfully created, but the combobox is empty, it's datasource is "Nothing", and will not allow it's .DataSource property to be set to anything but "Nothing" (I've stepped the code, and tried to enter the .DataSet property while debugging, but it snaps back to "Nothing".)

Here's a snippet of the code; it's pretty straight-forward:

        Private Sub addpersonButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addpersonButton.Click
            Dim dsEmployees As DataSet

                addpersonButton.Visible = False

                comboDataLoading = True

                dsEmployees = employees.GetAllEmpNames()

                addpersonComboBox.DataSource = dsEmployees.Tables(0)
                addpersonComboBox.DisplayMember = "FullName"
                addpersonComboBox.ValueMember = "EmpID"

                addpersonComboBox.Visible = True

                comboDataLoading = False
        End Sub


        Private Sub addpersonComboBox_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles addpersonComboBox.SelectedValueChanged
            If comboDataLoading = True Then
                Exit Sub
            End If

            addpersonComboBox.Visible = False

            comboDataLoading = True

            addpersonComboBox.DataSource = Nothing
            addpersonComboBox.DisplayMember = ""
            addpersonComboBox.ValueMember = ""
            addpersonComboBox.DataBindings.Clear()
            addpersonComboBox.Items.Clear()

            comboDataLoading = False

            addpersonButton.Visible = True
        End Sub


...That's it.  When the addpersonButton is clicked a second time, I can see in the debugger that the .DisplayMember and .ValueMember properties are set properly, but nothing I do will cause the combo's .DataSource property to be set to the dsEmployees dataset table.

Is this a bug in .Net?  You can see that I've tried clearing anything that could possibly have anything to do with databindings in the second procedure, but it doesn't change the behavior of this screwy thing.

Thanks in Advance for any help.
Avatar of carmodyk
carmodyk
Flag of United States of America image

Hmmm...Maybe a quick fix.  Try this:  instead of,  Dim dsEmployees As DataSet  Declare it as this Dim dsEmployees As new DataSet
Avatar of proten
proten

You are setting your addpersonComboBox.Visible = False but are never resetting it to true.  
Try adding addpersonComboBox.Visible = True in your click event.
Sorry misread
ignore my post
Sorry for posting so many times.

Try setting the addpersonComboBox.Visible = True before you set the datasource.
Avatar of ccoton

ASKER

Thanks for the replies.

Sorry about the confusion-  I have played with so many versions of the two procedures above that I didn't include the "addpersonComboBox.Visible = True" statement in the example.  In the original code, I do set the .Visible property of the combo to True.   Also, I have tried a version of the code with "Dim dsEmployees As New DataSet", with no change in the ComboBox behavior.

I did find a similar post while searching here-  Someone had a similar problem, but thought it was because he had his combo in a Tab Page.  I have mine in a Tab Page as well, but I moved it onto the parent form, with no change in behavior.  The solution he accepted was to iterate through the dataset and load the combo manually.  I may have to do this, but I really would like to know why the combo is behaving this way-  I hate to kludge things with work-arounds when the syntax is straight-forward and should work.
Avatar of ccoton

ASKER

proten-

Here's someting strange:  When I set the addpersonComboBox.Visible = True before setting the datasource, the combo came up empty the -first- time I brought it up.   Weird.
So correct me if I am wrong:

You have a combobox on a windows form (not in a datagrid or anything like that)
and you are setting the datasource AFTER the combobox is visible?

Are you sure that you are passing back a datatable in the dataset and that the dataset is not empty?
Avatar of ccoton

ASKER

No, the code as it stands now sets the combo's datasource, etc. BEFORE it's visible, when the user clicks a button.

I then set .Visible to True, and it works.

When the user selects something from the list, I use code in the combo's .SelectedValueChanged event to set the .DataSource to nothing, and to make the combo invisible.

When the user clicks the same button again, the same code is executed, a good dataset is generated once again, I set the datasource, etc. once again, but the combo is empty.  I double-checked the dataset at this point, and have good data in it, just like the first time.

Checking property values in the debugger shows that the combo's .Datasource property is now "Nothing" in this second go-around; even when I try to manually set this value back to the dataset, it snaps back to "Nothing".
Avatar of ccoton

ASKER

To add the post above:  The combo box is in a groupbox that sits on a tab page.

I moved the combo out onto the form; it exhibits the same behavior.
ASKER CERTIFIED SOLUTION
Avatar of Sancler
Sancler

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
Avatar of ccoton

ASKER

Roger-

Thanks, I'll give it a try tomorrow.  I'm about to go blind right now.  After searching the web for hours, I've run across other people that have had the same problem, as well as other PITA problems surrounding binding ComboBoxes.  Each of them, to a person, finally gave up and did everything manually.   I still intend to get to the bottom of this here, but until then, I used the following code to do it manually (using the same dataset from my first post):

________________
Loading:
            For Each dsRow In dsEmployees.Tables(0).Rows
                addpersonComboBox.Items.Add(New ListItem(dsRow.Item("EmpID").ToString(), dsRow.Item("FullName").ToString()))
            Next

Retrieving text and key:
            Dim selectedListItem As ListItem = DirectCast(addpersonComboBox.SelectedItem, ListItem)
            MessageBox.Show(selectedListItem.ID + ", " + selectedListItem.Name)

My ListItem class:

Public Class ListItem
    Private mID As String
    Private mName As String

    Public Sub New(ByVal ID As String, ByVal Name As String)
        mID = ID
        mName = Name
    End Sub

    Public Sub New()
        'Empty default constructor
    End Sub

    Public Property ID() As String
        Get
            Return mID
        End Get
        Set(ByVal Value As String)
            mID = Value
        End Set
    End Property

    Public Property Name() As String
        Get
            Return mName
        End Get
        Set(ByVal Value As String)
            mName = Value
        End Set
    End Property

    Public Overrides Function ToString() As String
        Return mName
    End Function
End Class
________________