我有一个字符串“test1”,我的组合框包含test1、test2和test3。如何设置选中项为“test1”?也就是说,我如何匹配我的字符串到一个组合框项目?
我想的是下面这行,但这不行。
comboBox1.SelectedText = "test1";
我有一个字符串“test1”,我的组合框包含test1、test2和test3。如何设置选中项为“test1”?也就是说,我如何匹配我的字符串到一个组合框项目?
我想的是下面这行,但这不行。
comboBox1.SelectedText = "test1";
当前回答
这对我有用.....
comboBox.DataSource.To<DataTable>().Select(" valueMember = '" + valueToBeSelected + "'")[0]["DislplayMember"];
其他回答
假设test1、test2、test3属于comboBox1集合,下面的语句将工作。
comboBox1.SelectedIndex = 0;
_cmbTemplates.SelectedText = "test1"
或者
_cmbTemplates.SelectedItem= _cmbTemplates.Items.Equals("test1");
ListItem li = DropDownList.Items.FindByValue("13001");
DropDownList.SelectedIndex = ddlCostCenter.Items.IndexOf(li);
你的案子可以用
DropDownList.Items.FindByText("Text");
如果组合框中的项目是字符串,您可以尝试:
comboBox1.SelectedItem = "test1";
comboBox1.SelectedItem.Text = "test1";