Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

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?

Edited by hsueh001
  • 2 weeks later...
Posted

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!!

Trust the Universe

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...