Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have project I and I have compobox , in the compobox I have data form table in database.

I use this compobox to insert to another table but I don't want the selected value I want the hidden value ( in other words in the compobox the shown word is " Name " but I want to insert the " ID " )

Thanks
Posted
Updated 18-Jun-12 9:53am
v2

The way I would do it is to create a class which held one row's worth of information, and override the ToString implementation to return just the information I wanted. Then when you use the ComboBox.SelectedItem property, you just cast it to the appropriate type:
C#
Video video = myComboBox.SelectedItem as Video;
if (video != null)
    {
    Console.WriteLine("{0}:{1}", video.Id, video.Title);
    }
 
Share this answer
 
compobox the shown word is " Name " but I want to insert the " ID "
Use the SelectedValue property for it.

Refer: MSDN: ListControl.SelectedValue Property [^]

Sample example:
C#
// Set the name as the property to be displayed and the ID as the value to be returned when a row is selected.  
// Here these are properties; if we were binding to a database table or query these could be column names.
ComboBox1.DisplayMember = "Name";
ComboBox1.ValueMember = "ID";

C#
// After selection
// this will give you the inner 'hidden' value
textBox1.Text = ComboBox1.SelectedValue.ToString();
// If you also wanted to get the displayed text
// the SelectedItem item property:
// string s = ((USState)ComboBox1.SelectedItem).Name;
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900