invalid cast exception
Hi,
I've got a little problem with my C# WinForms test application.
I would like to have a ComboBox showing a a visual value and some hidden values.
So I build a class able to store thoose values. This class called ListItem have got two private variable, one is a string containing the visual value, the
other one is a UDT variable. It is a struct called ValoriNascosti containing two variables a string and an int.
The costructor of ListItem store the three values (description, and the two of ValoriNascosti). There is two accessor methods that retrieve: the string of th
description; and another one retrieve a variable of type ValoriNascosti.
I create an ArrayList of ListItem in this way:
ArrayList elencoProdotti = new ArrayList();
elencoProdotti.Add(new ListItem("0001", 5, "Prodotto1"));
I populate the combo with the ArrayList:
this.comboBox1.DataSource = elencoProdotti;
Than I tell to the combo that the member to display is accessible from the method Descrizione:
comboBox1.DisplayMember = "Descrizione";
And the hidden value is accessible from the method AccessorValoriNascosti:
comboBox1.ValueMember = "AccessorValoriNascosti" ;
Thoose two are the accessors I was talking about.
When I run this I've got my dear combo with the visual data I've choose (like Prodotto1, Prodotto2, Prodotto3 etc...).
So I add code in the event SelectedIndexChanged, trying to show the hidden values when the combobox element changes. So I do this:
this.textBox1.Text = ((ValoriNascosti)this.comboBox1.SelectedValue).mod uli.ToString();
but I've got an invalid cast exception.
This line give the same error:
ValoriNascosti supporto = (ValoriNascosti)this.comboBox1.SelectedValue;
The value stored should be of valoriNascosti type... isn't right this way of casting?
I really don't have clues. :-Thanks for help.
Giulio
|