如何在运行时从带绑定的组合框中获取选定的文本 [英] How to get selected text from binded combobox with collection at run time

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

问题描述

你好

我在程序中有一个组合框,我用选择查询绑定了组合框

hello
i have a combobox in program and i binded combobox with select query

combogroup.DataSource = CNRL.ShowGroup();
combogroup.DisplayMember = "TitleGroup";
combogroup.ValueMember = "GroupID";



当我从组合中选择项目时,请参阅组合文本中的所选项目


when i select item from combo cant see selected item in combo text

combogroup.text = combogroup.selecteditem.tostring();

推荐答案

如果.ShowGroup()返回一个DataTable我想你的组合中的每一项都是一个DataRowView。



简单的方法是使用BindingSource控件,并使用.Current获取所选项目,如果不是:



首先:combo.DropDownStyle = DropDownList





要显示文字:(我假设你不是感兴趣的是.SelectedValue)



DataRowView view =(DataRowView)combo.SelectedItem;

string tx = view.Row [TitleGroup] .ToString();
If .ShowGroup() returns a DataTable I suppose every item of your combo is a DataRowView.

The easy way is using a BindingSource control, and using .Current to get the selected item, if not:

First: combo.DropDownStyle = DropDownList


To get displayed text: (I assume you're not interested in .SelectedValue)

DataRowView view = (DataRowView)combo.SelectedItem;
string tx = view.Row["TitleGroup"].ToString();


如果我理解你的问题,你想获得所选项目的文本,当选择改变时(?)



我建议你使用selectedindexchanged事件,有一个selectedvaluechange事件,但由于未知原因而不可靠。



在这里,您可以使用列表中非常好的索引(最可靠)来访问单个数据



https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindexchanged(v = vs.110).aspx [ ^ ]



如果你想访问所选文本的文本,那很大程度上取决于你绑定的内容,因为实现声明它是一个对象它可以是任何东西

https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindex(v = vs.110)的.aspx [ ^ ]



但是,Combobox是一个listcontrol,因此它支持两个属性displaymember和valuemember,它是你想要的displaymember属性,如果没有提供值displaymember,它将有一个值,在这种情况下会调用该对象的.ToString()方法。



因此,将显示成员提供给你的组合框,然后将你绑定的类型转换为项目,并访问这样命名的(属性),这在铸造之后应该是dotable。
If i understand your problem correctly, you want to get the text of the selected item, when a selection changes (?)

I recommend you use the selectedindexchanged event, there is a selectedvaluechange event, but it is for unknown reasons not as reliable.

Here you can use the very nice index (most reliable) in the list to access the individual data

https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindexchanged(v=vs.110).aspx[^]

and if you want to access the text of the selected text, it much depends on what you're binding on, as the implementation states that it is an object it could be anything
https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindex(v=vs.110).aspx[^]

However a Combobox is a listcontrol and as such it supports two properties "displaymember" and "valuemember", it is the displaymember property you want to if the value "displaymember" is not supplied it will have a value "" and in this case cause a call to the .ToString() method of the object.

So supply the displaymember to your combobox and then cast the item the type you're binding to and access the (property) named like that, this should be dotable after casting.


这篇关于如何在运行时从带绑定的组合框中获取选定的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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