Setting SelectedItem of ComboBox programatically not working

C

Crazy Cat

Hi all,

I have combobox that is bound to a custom object collection thusly

Dim collection As List(Of StructureType) =
StructureType.FindStructureTypes(SharedObjects.StructureTypes,
DropDownListStructure.SelectedValue, DropDownListType.SelectedValue)

DropDownListSubType.DataSource = collection
DropDownListSubType.DisplayMember = "StructureName"

The method FindStructureTypes is a shared method that returns a list
of StructureType objects filtered by the value in another combobox.

When I set the SelectedItem of the ComboBox programatically to a
StructureType object the selecteditem never changes.

Suspecting that the SelectedItem is not found in the combobox's
datasource I put in a piece of code to print the index of the object
in the collection using List(Of StructureType).IndexOf. As suspected I
get a -1.

When I run this code in debug and look at the collection (15 objects
in all) in QuickWatch I can see that the object is in the collection.
These objects are retrieved from a database and have a unique ID
property. The StructureType object that I am setting in the combobx
comes from the same list as the datasource using a method that
retrieves just one item by ID.

Has anyone seen this before, or am I missing something about
SelectedItem?

PLEASE HELP

Thanks
 
T

Tom Shelton

Hi all,

I have combobox that is bound to a custom object collection thusly

Dim collection As List(Of StructureType) =
StructureType.FindStructureTypes(SharedObjects.StructureTypes,
DropDownListStructure.SelectedValue, DropDownListType.SelectedValue)

DropDownListSubType.DataSource = collection
DropDownListSubType.DisplayMember = "StructureName"

The method FindStructureTypes is a shared method that returns a list
of StructureType objects filtered by the value in another combobox.

When I set the SelectedItem of the ComboBox programatically to a
StructureType object the selecteditem never changes.

Suspecting that the SelectedItem is not found in the combobox's
datasource I put in a piece of code to print the index of the object
in the collection using List(Of StructureType).IndexOf. As suspected I
get a -1.

When I run this code in debug and look at the collection (15 objects
in all) in QuickWatch I can see that the object is in the collection.
These objects are retrieved from a database and have a unique ID
property. The StructureType object that I am setting in the combobx
comes from the same list as the datasource using a method that
retrieves just one item by ID.

Has anyone seen this before, or am I missing something about
SelectedItem?

PLEASE HELP

Thanks

Is it the same actual object or is it an object with the same data?
 
C

Cor Ligthert [MVP]

Hi,

I thought that I had seen this more and that it is because are using the
SelectedItem however not the itemarray, why don't you use the itemarray for
this instead of a datasource. For a datasource you can use the
selectedindex. The combobox is combining working with datasources however
has as well as combination an itemarray in it. It is very much working
seperated from each other.

Cor
 
C

Crazy Cat

Is it the same actual object or is it an object with the same data?

Yes, Tom it is the same object. I get both the datasource and the item
I am attemping to set to the SelectedItem from a much larger
collection (using different methods but still it is run against the
same collection).

I ended up using the SelectedValue property. Thanks much.
 
C

Crazy Cat

Hi,

I thought that I had seen this more and that it is because are using theSelectedItemhowever not the itemarray, why don't you use the itemarray for
this instead of a datasource. For a datasource you can use the
selectedindex. The combobox is combining working with datasources however
has as well as combination an itemarray in it. It is very much working
seperated from each other.

Hi Cor,

I ended up using the SelectedValue instead of the SelectedItem.

Thanks,
 
T

Tom Shelton

Yes, Tom it is the same object. I get both the datasource and the item
I am attemping to set to the SelectedItem from a much larger
collection (using different methods but still it is run against the
same collection).

I ended up using the SelectedValue property. Thanks much.- Hide quoted text -

- Show quoted text -

Glad you found a solution.
 

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