This is a code I found from this link Hidden Id With ComboBox Items?.
class ComboBoxItem
{
string displayValue;
public string hiddenValue;
//Constructor
public ComboBoxItem (string d, string h)
{
displayValue = d;
hiddenValue = h;
}
//Accessor
public string HiddenValue
{
get
{
return hiddenValue;
}
}
//Override ToString method
public override string ToString()
{
return displayValue;
}
}
Using the below code
ComboBox.Items.Add(new ComboBoxItem("DisplayValue", "HiddenValue");
I was able to display the value that I expected to display. Lets say for example, I saved ComboBox.Items.Add(new ComboBoxItem("Mike", "1");. It displays Mike in the combobox without a problem. But now what I need, is to take the corresponding hiddenvalue from the combobox.
For that, I ran the below code.
string hValue = ((ComboBoxItem)ComboBox.SelectedItem).HiddenValue;
But in the runtime, its throwing me an NullReferenceException
. “Object reference not set to an instance of an object.”