ComboBox properties

V

vbtrying

Is it possible to have a combobox contain 2 fields from a database table --
having the 1st field displayed in the textbox area and HIDE the 2nd field?
When the user selects a value from the textbox, the hidden field is then
passed to the program (Ex: Have a combobox display the FULL state name, but
once the user selects a state, the state abbreviation is passed to the
program)

Thanks!
 
A

Alberto Poblacion

vbtrying said:
Is it possible to have a combobox contain 2 fields from a database
table --
having the 1st field displayed in the textbox area and HIDE the 2nd field?
When the user selects a value from the textbox, the hidden field is then
passed to the program (Ex: Have a combobox display the FULL state name,
but
once the user selects a state, the state abbreviation is passed to the
program)

Yes, the ComboBox accepts any kind of Object for its elements, and
displays the ToString() of each object. You can create a class to
encapsulate your data and load the combo with objects of that class:

class MyData
{
public string Code { get; set; }
public string State { get; set; }
public MyData(string code, string state) { Code=code; State=state; }
public override string ToString() { return State; }
}
....

ComboBox1.Items.Add(new MyData("TX", "Texas"));
ComboBox1.Items.Add(new MyData("WA", "Washington"));
....
string selectedCode = ((MyData)ComboBox1.SelectedItem).Code;
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


vbtrying said:
Is it possible to have a combobox contain 2 fields from a database
table --
having the 1st field displayed in the textbox area and HIDE the 2nd field?
When the user selects a value from the textbox, the hidden field is then
passed to the program (Ex: Have a combobox display the FULL state name,
but
once the user selects a state, the state abbreviation is passed to the
program)

What you mean with passed to the program?

Combobox bind to an object[] and use ToString() to display the value. What
you can do is create a new class a la ListViewItem with a Tag property. You
can then assign the secnod column or even the Row itself and have it readily
accesible when needed.
 

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