Friday 18 August 2006

Winforms Combo Box and Sorted Property

Scott

Had a very strange one the other day ... the conclusion is the Sorted property of the forms.combobox control should never be true when the combo box is using a datasource.

The combo box was set up no different - Set the DataSource with a DataRow[], Set the DisplayMember and ValueMember. The DataRow[] was already in the desired Description ASC sort order;

IDDescription
302Alopecia
345Appetite Decreased
346Appetite Increased
303Behavioural Abnormality
304Bleeding
313Dystocia

So with the DisplayMember = Description column, the ValueMember = ID column and the Sorted property set to false.

As expected the SelectedValue returns the expected ID value.

However, with the DisplayMember = Description column, the ValueMember = ID column and the Sorted property set to true. The combo box data actually is;

IDDescription
302Alopecia
303Appetite Decreased
304Appetite Increased
305Behavioural Abnormality
306Bleeding
307Dystocia

So the ValueMember has begun from the first ID value and just incremented as each item was bound.

In this case the use of SelectedItem.ID returns the correct ID value.

So just my experience ... maybe obvious ... but i have not seen this documented anywhere and surely i cant be the first person to have experienced it ?!?!?!!?

No comments: