问题描述
嗨我有一个具有绑定源作为数据源的组合框.在绑定源中,有一个对象(材料对象)的集合.
我绑定到组合框的selectedValue属性的主绑定源包含一个引号线对象.引号线的属性之一是嵌入的Material对象.
因此,我希望显示一个Material对象列表的组合框,并且当用户从组合框中选择Material时,Quoteline的Material属性将使用所选对象进行更新.一切正常,除了组合框的初始选定项.它只是停放在列表中的第一个对象上.这是我的代码.请帮忙.
HiI have a combobox with a bindingsource as a datasource. Inside the bindingsource, there is a collection of objects (Material object).
My main binding source which is bound to the combobox''s selectedvalue property contains a quoteline object. One of the properties of quoteline, is an embedded Material object.
I would therefore want a combobox displaying a list of Material objects and when the user selects a Material from the combobox, the Material property of the Quoteline is updated with the selected object. Everthing works fine, except the initial selected item of the combobox. It just parks on the 1st object in the list. Here is my code. Please help.
Material material;
Material material2;
public Form1()
{
InitializeComponent();
prematerial = new Material();
prematerial.MaterialId = 0;
prematerial.MaterialDesc = "Pine";
bindingSource1.Add(prematerial);
material = new Material();
material.MaterialId = 1;
material.MaterialDesc = "Kiaat";
bindingSource1.Add(material);
material2 = new Material { MaterialId = 2, MaterialDesc =
Den;};
bindingSource1.Add(material2);
comboBox1.DataSource = bindingSource1;
comboBox1.DisplayMember = "MaterialDesc";
quoteline = new QuoteLine { Material = material2 };
bsMaster.Add(quoteline);
comboBox1.DataBindings.Add(new Binding("SelectedValue", bsMaster, "Material", true));
}
推荐答案
comboBox1.SelectedIndex = 3;
@-Abdul Quader Mamun-
没有数据源就可以提供帮助-创建自己的数据-花了我30秒钟的时间.我认为,为了演示这个问题,您只需要一个数据绑定组合框即可. :rose:
@ - Abdul Quader Mamun -
Its possible to help without data source - create your own - took me 30 secs. I think to demonstrate the problem you just need a databound combobox. :rose:
这篇关于组合框的初始绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!