Bobski Bobski - 1 year ago 88 Question

Performing an action when a combobox value is selected

I'm having some trouble doing something that seems very simple but I just don't know what I'm doing wrong....

On Form_Load I populate a combobox with


Here I set .selectedindex = - 1 so that nothing is selected. The combobox gets populated properly and everything is bells and whistles.

Now when user selects something from the drop down, I want to populate a DataGrid with a bunch of info based off the VALUE selected. So I pass and ID into the function that populates the DataGrid.

I'm trying to do this like this...

Private Sub cbo1_SelectedValueChanged(sender As Object, e As EventArgs) Handles cbo1.SelectedValueChanged
dim productID as string=""

cbo1.SelectedValue = ProductID
Call PopulateProductGrid(ProductID)
End Sub

I also have tryined with SelectedIndexChanged, but for both event I get an error

Cannot set the SelectedValue in a ListControl with an empty

Answer Source

I'll wager that the issue is that you're binding in the wrong order. You should always set the DataSource last, not first. Try changing that and see if your issue goes away. The problem is that setting the DataSource means that the control is bound, so an item will be selected and the appropriate events raised before you have set the DisplayMember and ValueMember.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download