在绑定到数据源的组合框上设置SelectedItem [英] Set SelectedItem on a combobox bound to datasource

查看:174
本文介绍了在绑定到数据源的组合框上设置SelectedItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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...

推荐答案

p>

You should do

comboBox.SelectedValue = "valueToSelect";

comboBox.SelectedIndex = n;

comboBox.Items[n].Selected = true;

这篇关于在绑定到数据源的组合框上设置SelectedItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆