分配数据绑定组合框的文本 [英] Assign the Text of a data-bound ComboBox
问题描述
我想分配一个数据绑定的ComboBox的文本.我尝试设置Text
,SelectedValue
和SelectedItem
属性,但是它们似乎都不起作用.我将对如何最好地解决此问题的建议和技巧表示赞赏.
它们似乎都不起作用,这意味着为文本框分配值的工作"还没有完成.
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 theItems
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 displayText
property when you select an item. AndSelectedValue, SelectedItem
will have value(s) when only if you setDataSource, 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屋!