分配数据绑定组合框的文本 [英] Assign the Text of a data-bound ComboBox

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

问题描述

我想分配一个数据绑定的ComboBox的文本.我尝试设置TextSelectedValueSelectedItem 属性,但是它们似乎都不起作用.我将对如何最好地解决此问题的建议和技巧表示赞赏.
它们似乎都不起作用,这意味着为文本框分配值的工作"还没有完成.

I want to assign the Text of a data-bound ComboBox. I tried setting the Text, SelectedValue, and SelectedItem properties but none of them seem to work. I would appreciate suggestions and tips on how best to approach this problem.
none of them seem to work means My Work of Assigning Value to TextBox is Not Done By It.

推荐答案

如果您要提供ComboBox的内容下拉菜单,则需要使用Items属性.

If you mean that you want to provide the contents of the ComboBox dropdown then you need to use the Items property.

myComboBox.Items.Add("Some Text");
myComboBox.Items.Add("Some More Text");






or

string[] items = new string[] { "Some Text", "Some More Text" };
myComboBox.Items.AddRange(items);



如果这不是您想要的,那么您将不得不编辑您的问题以使您的要求更加清楚.



If this is not what you want then you will have to edit your question to make your requirements clearer.


astika,

选择一个项目时,ComboBox将显示Text属性.仅当您设置DataSource, DisplayMember, ValueMember属性时,SelectedValue, SelectedItem才具有值.请尝试:

Hi astika,

ComboBox will display Text property when you select an item. And SelectedValue, SelectedItem will have value(s) when only if you set DataSource, DisplayMember, ValueMember property. Please try:

cbo.DataSource = dataSet;//Or dataTable
cbo.DisplayMember = "FieldDisplayOfDataset";
cbo.ValueMember = "FieldValueOfDataset";



更新:
像这样分配给ComboBox时,可以通过设置SelectedValue来更改Text.它将更改控件上的Text显示.

例如:



Update:
When you assigned to ComboBox like that, you can change the Text by set SelectedValue. It will be change the Text display on the control.

example:

cbo.SelectedValue = "ValueExistInField";


这篇关于分配数据绑定组合框的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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