Unable to set combo box selected item index to -1

B

B

I'm trying to simply build a form with a combo box containing a list of
states. I'd like for there to be NO default selected item, but invariably,
the first item in the DataSource is being displayed. It seems to work fine
on other forms, but for one particular form, nothing I do seems to work. My
code is basically:

string SQL = "";

SQL = "SELECT StateAbbreviation FROM States ORDER BY StateAbbreviation";

//The Helper method simply builds a DataSet from the provided SQL query. I
then
//set the combo datasource to the first table within the DataSet

this.cboOrigin.DataSource = Helpers.CreateDataSetFromSQL(SQL).Tables[0];
this.cboOrigin.DisplayMember = "StateAbbreviation";
this.cboOrigin.ValueMember = "StateAbbreviation";
this.cboOrigin.SelectedIndex = -1;

If I place an OnSelectedIndexChanged() event handler on the combo box, it
appears as if the index is getting set properly, but ultimately, the first
state abbreviation still appears by default. This is all happening within a
Form_Load() method, and the combo is just a plain
System.Windows.Forms.ComboBox control with the DropDownStyle property set to
DropDownList. Any ideas?
 
M

MuZZy

B said:
I'm trying to simply build a form with a combo box containing a list of
states. I'd like for there to be NO default selected item, but invariably,
the first item in the DataSource is being displayed. It seems to work fine
on other forms, but for one particular form, nothing I do seems to work. My
code is basically:

string SQL = "";

SQL = "SELECT StateAbbreviation FROM States ORDER BY StateAbbreviation";

//The Helper method simply builds a DataSet from the provided SQL query. I
then
//set the combo datasource to the first table within the DataSet

this.cboOrigin.DataSource = Helpers.CreateDataSetFromSQL(SQL).Tables[0];
this.cboOrigin.DisplayMember = "StateAbbreviation";
this.cboOrigin.ValueMember = "StateAbbreviation";
this.cboOrigin.SelectedIndex = -1;

If I place an OnSelectedIndexChanged() event handler on the combo box, it
appears as if the index is getting set properly, but ultimately, the first
state abbreviation still appears by default. This is all happening within a
Form_Load() method, and the combo is just a plain
System.Windows.Forms.ComboBox control with the DropDownStyle property set to
DropDownList. Any ideas?

It's a known bug in .NET 1.1 - install .NET 1.1 Service Pack 1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top