如何从组合框读取 [英] How to read from Combobox

查看:71
本文介绍了如何从组合框读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我有一个组合框.

Dear All,

I have a combobox.

cmbgenric.datasource = datatable("tbl")
cmbgenric.Valuemember = "GenricKey"
cmbgenric.displaymember= "GenricName"



不,我在Combobox中有5条记录.如果从组合框中选择3,我希望从valuemember中获得相应的值.

请帮助

谢谢和问候,
Mohi

以下文字是从OP发布的非解决方案中复制的[/编辑]

亲爱的所有人,

有没有人可以帮助我从combobox的valuemember中读取值.

问候,
Mohi



No i have 5 records in Combobox. If select 3 from the combobox, i want the corresponding value from the valuemember.

Please Help

Thanks and Regards,
Mohi

The following text copied from the non Solution posted by OP [/Edit]

Dear All,

Is there anyone out there who can help me in reading the values from valuemember of combobox.

Regards,
Mohi

推荐答案

您可以使用 ^ ]来获取您在组合框中当前选中的项目.

由于您正在使用数据表,因此项目本身应为DataRowView.因此,您可以使用项目 [
You can use the ComboBox.SelectedItem Property [^] to fetch the item that is currently selected in your comnbo box.

Since you''re using a datatable, the item itself should be a DataRowView. So you can reference the value using the Item[^] property.

So something like:
if (cmbgenric.SelectedItem != null) {
   ...((DataRowView)cmbgenric.SelectedItem)["GenricKey"]...
}


更新问题的答案:有没有人可以帮助我从combobox的valuemember中读取值.如下:

ComboBoxSelectedValue 属性可用于获取包含由ValueMember 属性指定的数据源成员的value 的对象,如此处所述
ListControl.SelectedValue [ ComboBox类 [ ^ ]源自ListControl ComboBox上可用.

因此,获取价值
The answer for the update to question: Is there anyone out there who can help me in reading the values from valuemember of combobox. can be as follows:

The SelectedValue property of ComboBox can be used to obtain the object containing the value of the member of the data source specified by the ValueMember property as explained here
ListControl.SelectedValue [^]. Even though it is written here as Listcontrol, as ComboBox class[^]is derived from ListControl it is available on ComboBox.

So to get the value
string value = cmbgenric.SelectedValue != null ? 
                      cmbgenric.SelectedValue.ToString() : string.Empty;



或者,如果ValueMember 指定的数据不是string type,则说double type,然后



Or if the data specified by the ValueMember is other than string type say double type then

double value =Convert.ToDouble(cmbgenric.SelectedValue);


Convert.ToDoublecmbgenric.SelectedValuenull 且不引发异常时,返回0.


Convert.ToDouble returns 0 when the cmbgenric.SelectedValue is null and does not throw exception.


将a视作字符串
a = cmbgenric.selectedvalue
dim a as string
a = cmbgenric.selectedvalue


这篇关于如何从组合框读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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