Inheritance Problem

hsueh001

Newcomer
Joined
Dec 5, 2005
Messages
8
This is for a WinForms environment.

I am trying to create a ComboBox, that contains all the Countries in my database where I don't have to "hook" it up on every single form that it will be used.

So I tried to do some inheritance, I inherited it from a ComboBox.
In the constructor I simply connect to the Sql database and query a list of Id and country values.

However when I drag this ComboBox into my WinForm I'm getting some code generation in the Designer.cs under the InitializeComponent().

The problem is the autocode generator generates two lines.
this.comboBoxCountry.DataSource = ((object)(resources.GetObject("comboBoxCountry.DataSource")));

AND
this.comboBoxCountry.Items.AddRange(new object[] {
((object)(resources.GetObject("comboBoxCountry.Items")))});

The order they are generated cause the second line to error.
If I move the second line above the first, the error disappears.

The error that I'm getting is:
"Items collection cannot be modified when the DataSource property is set."

Is there any way around this?
 
Last edited:
Maybe i am misunderstanding you but this could be a solution.

You could programmatically fill a dataset with the country values and id's and afterwards you can bind this dataset to the combobox.

In this way you can first set the datasource property of the combobox and bind the dataset afterwards.


Hope it helps!!
 
Back
Top