具有显示和隐藏值的C#中的compobox [英] compobox in c# with disply and hidden value

查看:107
本文介绍了具有显示和隐藏值的C#中的compobox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个项目,我有一个compobox,在compobox中,我的数据库中有数据表格.

我使用此compobox插入到另一个表中,但是我不希望选择的值是我想要的隐藏值(换句话说,在compobox中,显示的单词是"Name",但我想插入"ID" ;)

谢谢

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

推荐答案

我要做的方法是创建一个保存一行信息的类,并重写ToString实现以仅返回信息我想了.然后,当您使用ComboBox.SelectedItem属性时,只需将其转换为适当的类型即可:
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:
Video video = myComboBox.SelectedItem as Video;
if (video != null)
    {
    Console.WriteLine("{0}:{1}", video.Id, video.Title);
    }


compobox,显示的单词是名称",但我想插入"ID"
使用SelectedValue属性.

参考: MSDN:ListControl.SelectedValue属性 [ ^ ]

示例示例:
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:
// 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";


// 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;


这篇关于具有显示和隐藏值的C#中的compobox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆