如何在 C# Windows 窗体中设置 ComboBox 的选定项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9396117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set Selected item of ComboBox in C# Windows Forms?
提问by Azhar
I am trying to set selected item of comboBoxon click event of DataGrid, but I could not. I have googled and tried different ways but without success.
我正在尝试设置comboBoxDataGrid 的点击事件的选定项目,但我不能。我用谷歌搜索并尝试了不同的方法,但没有成功。
For me SelectedIndexis working, but I could not find the index of items in ComboBox, so I could not select the item.
对我来说SelectedIndex正在工作,但我在 ComboBox 中找不到项目的索引,所以我无法选择该项目。
Not working code:
不工作的代码:
for (int i = 0; i < cmbVendor.Items.Count; i++)
if (cmbVendor.Items[i].ToString() == Convert.ToString(gridView1.GetFocusedRowCellValue("vVendor")))
{
cmbVendor.SelectedIndex = i;
break;
}
采纳答案by Kamil
You can get your item index by the .Items.IndexOf()method. Try this:
您可以通过该.Items.IndexOf()方法获取您的项目索引。尝试这个:
comboBox1.SelectedIndex = comboBox1.Items.IndexOf(gridView1.GetFocusedRowCellValue("vVendor"));
You don't need to iterate.
你不需要迭代。
You can find more information in Stack Overflow question How do I set the selected item in a comboBox to match my string using C#?.
您可以在 Stack Overflow 问题中找到更多信息如何使用 C# 在组合框中设置所选项目以匹配我的字符串?.
回答by Steve
You have it in your if:
如果:
cmbVendor.SelectedItem = cmbVendor.Items[i];
回答by b0rg
Assuming gridView1.GetFocusedRowCellValue("vVendor")really works as expected, the following code should fix the problem.
假设gridView1.GetFocusedRowCellValue("vVendor")确实按预期工作,以下代码应该可以解决问题。
string selected = Convert.ToString(gridView1.GetFocusedRowCellValue("vVendor"));
foreach ( var item in cmbVendor.Items )
{
if (string.Compare(item.ToString(), selected, StringComparison.OrdinalIgnoreCase) == 0)
{
cmbVendor.SelectedItem = item;
break;
}
}
The original code had multiple calls to gridView1.GetFocusedRowCellValue("vVendor"), whereas you only need one.
原始代码多次调用gridView1.GetFocusedRowCellValue("vVendor"),而您只需要一次。
The suggested "comboBox1.Items.IndexOf(" assumes too much about the content of cmbVendor.Items.
建议的“comboBox1.Items.IndexOf(”对cmbVendor.Items.
回答by Azhar
At last I found it out. It's:
最后我发现了。它的:
cmbVendor.Text = Convert.ToString(gridView1.GetFocusedRowCellValue("vVendor"));
The SelectedText property is for the selected portion of the editable text in the textbox part of the combo box.
SelectedText 属性用于组合框文本框部分中可编辑文本的选定部分。
回答by ashish Patel
The following is working for me perfectly. Pass any value or Text which is available in the combobox.
以下对我来说是完美的。传递组合框中可用的任何值或文本。
comboBox1.SelectedIndex = comboBox1.FindString(<combobox value OR Text in string formate>);
回答by AJameson56
I had a similar problem and worked it out partially with the help of the other answers here. First, my particular problem was that
我有一个类似的问题,并在其他答案的帮助下部分解决了这个问题。首先,我的特殊问题是
combobox1.SelectedItem = myItem;
was not working as expected. The root cause was that myItem was an object from a group which was effectively the same list as the items in the combobox, but it was actually a copy of those items. So myItem was identical to a valid entry, but itself was not a valid object from the combobox1 container.
没有按预期工作。根本原因是 myItem 是一个组中的对象,该组实际上与组合框中的项目相同,但它实际上是这些项目的副本。因此 myItem 与有效条目相同,但它本身不是来自 combobox1 容器的有效对象。
The solution was to use SelectedIndex instead of SelectedItem, like this:
解决方案是使用 SelectedIndex 而不是 SelectedItem,如下所示:
combobox1.SelectedIndex = get_combobox_index(myItem);
where
在哪里
private int get_combobox_index(ItemClass myItem)
{
int i = 0;
var lst = combobox1.Items.Cast<ItemClass >();
foreach (var s in lst)
{
if (s.Id == myItem.Id)
return i;
i++;
}
return 0;
}
回答by Jaydeep Karena
this works for me.....
这对我有用.....
string displayMember = ComboBox.DataSource.To<DataTable>().Select("valueMemberColumn = '" + value + "'")[0]["displayMember"].ToString();
ComboBox.FindItemExact(displayMember, true).Selected = true;
回答by chaosifier
If you have set ValueMember property for the ComboBox control, you can simply assingn the Value to the ComboBox control's SelectedValue property. You don't have to find the index explicitly. Here's an example:
如果您已经为 ComboBox 控件设置了 ValueMember 属性,您可以简单地将 Value 分配给 ComboBox 控件的 SelectedValue 属性。您不必显式地查找索引。下面是一个例子:
public class Vendor{
public int VendorId {get; set;}
public string VendorName {get; set;}
}
// Inside your function
var comboboxData = new List<Vendor>(){
new Vendor(){ vendorId = 1, vendorName = "Vendor1" },
new Vendor(){ vendorId = 2, vendorName = "Vendor2" }
}
cmbVendor.DataSource = comboboxData;
cmbVendor.DisplayMember = "VendorName";
cmbVendor.ValueMember = "ValueId";
// Now, to change your selected index to the ComboBox item with ValueId of 2, you can simply do:
cmbVendor.SelectedValue = 2;
回答by Muhammad Sohail
ComboBox1.SelectedIndex= ComboBox1.FindString("Matching String From DataGrid Cell value")
Try this this will work fine in C# Windows application
试试这个,这将在 C# Windows 应用程序中正常工作

