我有一个字符串“test1”,我的组合框包含test1、test2和test3。如何设置选中项为“test1”?也就是说,我如何匹配我的字符串到一个组合框项目?
我想的是下面这行,但这不行。
comboBox1.SelectedText = "test1";
我有一个字符串“test1”,我的组合框包含test1、test2和test3。如何设置选中项为“test1”?也就是说,我如何匹配我的字符串到一个组合框项目?
我想的是下面这行,但这不行。
comboBox1.SelectedText = "test1";
当前回答
请试试这个方法,它对我很有效:
Combobox1.items[Combobox1.selectedIndex] = "replaced text";
其他回答
这应该可以达到目的:
Combox1.SelectedIndex = Combox1.FindStringExact("test1")
如果你通过数据集绑定数据源,那么你应该使用"SelectedValue"
cmbCategoryList.SelectedValue = (int)dsLookUp.Tables[0].Select("WHERE PRODUCTCATEGORYID = 1")[0]["ID"];
ComboBox1.SelectedIndex= ComboBox1.FindString("Matching String");
在windows窗体中尝试此操作。
在组合框中找到mySecondObject (MyObject类型)(包含MyObjects列表)并选择该项:
foreach (MyObject item in comboBox.Items)
{
if (item.NameOrID == mySecondObject.NameOrID)
{
comboBox.SelectedItem = item;
break;
}
}
我已经填充了我的组合框与从数据库填充的数据表。然后我设置了DisplayMember和ValueMember。我使用这段代码来设置所选项目。
foreach (DataRowView Row in ComboBox1.Items)
{
if (Row["ColumnName"].ToString() == "Value") ComboBox1.SelectedItem = Row;
}