How do i bind combobox to SortedList RRS feed

  • Question

  • I'm using object binding in my project. And i have successfly bindend some textboxes and datetimecontrols and now i got to point where i want to bind my comboBox and i'm stuck, cuz i can't do it

    Object i am binding to is: DateCurrencyDetailsManager

    This is the property of my object that i want to bind to

            public SortedList<int, string> PeriodList
                get { return _periodList; }
                    if(!_periodList.Equals (value))
                        _periodList = value;

                        if(PropertyChanged != null)
                            PropertyChanged (this, new PropertyChangedEventArgs ("PeriodList"));

    I have DataSource propery where i'm setting datasource of my comboBox

            public object DataSource
                    return dateCurrencyDetailsManagerBindingSource.DataSource;
                    dateCurrencyDetailsManagerBindingSource.DataSource = value;
                    if(value != null && value is DateCurrencyDetailsManager)
                        comboBoxPeriod.DataSource = ((DateCurrencyDetailsManager)value).PeriodList;

    When i run app, i get runtime ArgumentException:

    System.ArgumentException was unhandled by user code
      Message="Complex DataBinding accepts as a data source either an IList or an IListSource."
           at System.Windows.Forms.ListControl.set_DataSource(Object value)
           at System.Windows.Forms.ComboBox.set_DataSource(Object value)
           at Rex.Food.Details.DateCurrencyDetails.set_DataSource(Object value) in C:\if\dev\src\Rex\Food\Food.Meal\Details\DateCurrencyDetails.cs:line 26
           at Rex.Food.Details.FoodForm.SetParameters(Guid policyActionGuid, Guid clientGuid, Object policyAction, Boolean isLastPolicyAction, Int32 productLobOid, Boolean makeCopy, Boolean isEaglePolicy) in C:\if\dev\src\Rex.\Food\Food.Meal\Details\FoodForm.cs:line 86
           at Rex.Common.Search.UIManager.OpenPolicyForm(Object policyAction) in C:\if\dev\src\Rex\Food\Food.Common\Search\FoodUIManager.cs:line 537
           at Rex.Common.Search.UIManager.NewPolicy_Click(Object sender, EventArgs e) in C:\if\dev\src\Rex\Food\Food.Common\Search\FoodUIManager.cs:line 184
           at System.Windows.Forms.Control.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
           at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ButtonBase.WndProc(Message& m)
           at System.Windows.Forms.Button.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

    Monday, September 4, 2006 11:54 AM


  • Mix,
    the problem is exactly as described in the exception: you can only bind to a collection that implements IList or IListSource. Unfortunately, SortedList<> implements neither, and even the SortedList<>.Values property returns an IList<>, which does not match IList.

    The only workaround I can come up with at the moment is something like the following:

    public string [] StringValues {
      get { return new List<string> (PeriodList.Values).ToArray (); }

    You can now bind your combo to StringValues. Performance is not too bad, as the property is called only when you set your DataSource, which shouldn't happen too often.



    Wednesday, September 6, 2006 12:22 AM