Sayse
Sayse

Reputation: 43330

Set Combobox Items and DataSource

I'm having a problem where every time I build my solution, the compile succeeds but when I run my program it will error as the forms designer.cs file has had the data source for my custom comboboxes added to it automatically; resulting in an exception stating

Items collection cannot be modified when the DataSource property is set.

Any ideas on what might be the problem? I've tried setting the data source after the initialize component method but this results in a different error as the unit type is null..

The type of data source is set in a property for the control and below is the relevant code

form.Designer.cs (this is generated for you not a custom cs file called designer)

// 
// cmbWheelUnitCR
// 
this.cmbWheelUnitCR.DataSource = ((object)(resources.GetObject("cmbWheelUnitCR.DataSource")));
this.cmbWheelUnitCR.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cmbWheelUnitCR.FormattingEnabled = true;
this.cmbWheelUnitCR.Items.AddRange(new object[] {
"mm",
"yd"});

My custom combobox

public string UnitType
{
   get { return m_unitType; }
   set { m_unitType = value;
   this.DataSource = Units.Instance.UnitTypes(m_unitType);}
}
public UnitComboBox()
{
   InitializeComponent();
}

Upvotes: 1

Views: 2310

Answers (1)

Sayse
Sayse

Reputation: 43330

I was able to solve this accidentally by setting the data source within an override onLoad event..

Upvotes: 2

Related Questions