views:

184

answers:

1

In a windows forms project, I have several combo boxes. The first combo box contains a list of objects. Those objects then have several lists which are used as the datasource's for the successive combo boxes, depending on which item is chosen in the first.

When the first item is selected, the other combo boxes update correctly. When this item is changed a second time (or any successive time), the other combo boxes do not correctly update. When debugging, it shows that the other combo boxes are having their datasource assigned correctly, with the right 'count' of items. But the items aren't actually displaying. What could be the cause?

Private Sub cmbPackage_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPackage.SelectedIndexChanged
    // The Package controls what other options are available. 
    If (TypeOf (cmbPackage.SelectedItem) Is Package) Then
        // Set current package as a member object to see if it was a scoping issue
        _currentPackage = CType(cmbPackage.SelectedItem, Package)
        ClearOptionDropdowns()
        cmbReward.DataSource = _currentPackage.Rewards
        cmbPayment.DataSource = _currentPackage.PaymentTypes
        cmbCommMethod.DataSource = _currentPackage.CommunicationMethods
        cmbBillMethod.DataSource = _currentPackage.BillMethods
        cmbNotification.DataSource = _currentPackage.BillNotifications

        cmbReward.Refresh()
        cmbPayment.Refresh()
        cmbCommMethod.Refresh()
        cmbBillMethod.Refresh()
        cmbNotification.Refresh()
        ...
        ...
End Sub

Private Sub ClearOptionDropdowns()
    cmbReward.DataSource = Nothing
    cmbPayment.DataSource = Nothing
    cmbCommMethod.DataSource = Nothing
    cmbBillMethod.DataSource = Nothing
    cmbNotification.DataSource = Nothing
    ' Also had x.items.clear(), but removed to see if that was affecting it
End Sub
A: 

Using Reflector to see what happens when the datasource is set, the following method is called:

base.DataSource = value;

Which then does this:

if (this.dataSource != value)
    {
        try
        {
            this.SetDataConnection(value, this.displayMember, false);
        }

SetDataConnection:

private void SetDataConnection(object newDataSource, BindingMemberInfo newDisplayMember, bool force)
{
    bool flag = this.dataSource != newDataSource;
    bool flag2 = !this.displayMember.Equals(newDisplayMember);
    if (!this.inSetDataConnection)
    {
        try
        {
            if ((force || flag) || flag2)
            {
               // Update Accordingly

If I manually set the DisplayMember with a Guid.NewGuid.ToString, the datasource updates correctly. It appears that the datasource comparison is determining that they are the same when they most definately are not. Even when I call .DataSource = new List(Of ...)(list) it will not update correctly.

The Object.Equals method does this:

 InternalEquals(object objA, object objB);

Which I'm assuming does a pointer comparison? Are there any optimizations that the compiler might be doing that would cache one List object, and then fill that list with the required values at run-time? I'm certain that each List is a new List(Of type) when the objects are created.

Josh Smeaton