C# 在绑定到数据源的组合框上设置 SelectedItem

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10015942/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:08:30  来源:igfitidea点击:

Set SelectedItem on a combobox bound to datasource

c#comboboxdatasourceselecteditem

提问by mdc

List<Customer> _customers = getCustomers().ToList();
BindingSource bsCustomers = new BindingSource();
bsCustomers.DataSource = _customers;
comboBox.DataSource = bsCustomers.DataSource;
comboBox.DisplayMember = "name";
comboBox.ValueMember = "id";

Now how do I set the combobox's Item to something other than the first in the list? Tried comboBox.SelectedItem = someCustomer; ...and lots of other stuff but no luck so far...

现在如何将组合框的 Item 设置为列表中第一个以外的其他内容?试过 comboBox.SelectedItem = someCustomer; ......还有很多其他的东西,但到目前为止还没有运气......

采纳答案by Claudio Redi

You should do

你应该做

comboBox.SelectedValue = "valueToSelect";

or

或者

comboBox.SelectedIndex = n;

or

或者

comboBox.Items[n].Selected = true;

回答by Olivier Jacot-Descombes

Your binding code is not complete. Try this:

您的绑定代码不完整。尝试这个:

BindingSource bsCustomers = new BindingSource();
bsCustomers.DataSource = _customers;

comboBox.DataBindings.Add(
    new System.Windows.Forms.Binding("SelectedValue", bsCustomers, "id", true));
comboBox.DataSource = bsCustomers;
comboBox.DisplayMember = "name";
comboBox.ValueMember = "id";


In most cases you can accomplish this task in the designer, instead of doing it in code.

在大多数情况下,您可以在设计器中完成此任务,而不是在代码中完成。

Start by adding a data source in the "Data Sources" window in Visual Studio. Open it from menu View > Other Windows > Data Sources. Add an Object data source of Customertype. In the Data Sources you will see the customer's properties. Through a right click on the properties you can change the default control associated to it.

首先在 Visual Studio 的“数据源”窗口中添加数据源。从菜单View > Other Windows > Data Sources打开它。添加Customer类型为 Object 的数据源。在数据源中,您将看到客户的属性。通过右键单击属性,您可以更改与其关联的默认控件。

Now you can simply drag a property from the Data Sources window to you form. Visual Studio automatically adds A BindingSourceand a BindingNavigatorcomponent to your form when you drop the first control. The BindingNavigatoris optional and you can safely remove it, if you don't need it. Visual Studio also does all the wire-up. You can tweak it through the properties window. Sometimes this is required for combo boxes.

现在,您只需将属性从“数据源”窗口拖到表单中即可。当您放置第一个控件时,Visual Studio 会自动将 ABindingSource和一个BindingNavigator组件添加到您的窗体中。这BindingNavigator是可选的,如果您不需要它,您可以安全地删除它。Visual Studio 还负责所有接线工作。您可以通过属性窗口对其进行调整。有时这是组合框所必需的。

There's only one thing left to do in your code: Assign an actual data source to the binding source:

在您的代码中只剩下一件事要做:将实际数据源分配给绑定源:

customerBindingSource.DataSource = _customers;

回答by Raihan Al-Mamun

this works for me

这对我有用

bsCustomers.Position = comboBox.Items.IndexOf(targetCustomer);