简体   繁体   English

c #databound ComboBox:InvalidArgument =值'1'对'SelectedIndex'无效

[英]c# databound ComboBox : InvalidArgument=Value of '1' is not valid for 'SelectedIndex'

I'm having problems setting the SelectedIndex on a bound ComboBox (on a windows form) that I'm adding to a form at runtime and I suspect there's something odd going on. 我在绑定的ComboBox(在Windows窗体上)上设置SelectedIndex时遇到问题,我在运行时添加到表单中,我怀疑有些奇怪的事情发生了。

When I try this, I get the error "InvalidArgument=Value of '1' is not valid for 'SelectedIndex'." 当我尝试这个时,我得到错误“InvalidArgument ='1'的值对'SelectedIndex'无效。”

private void Form1_Load(object sender, EventArgs e)
        {
            List<string> comboBoxList = new List<string>();
            comboBoxList.Add("Apples");
            comboBoxList.Add("Oranges");
            comboBoxList.Add("Pears");

            ComboBox comboBox1 = new ComboBox();
            comboBox1.DataSource = comboBoxList;
            comboBox1.SelectedIndex = 1;
            this.Controls.Add(comboBox1);
        }

However, there is no problem if I add the items to the ComboBox directly, like this: 但是,如果我直接将项添加到ComboBox,则没有问题,如下所示:

comboBox1.Add("Apples");

Also, there is no problem if I add the control to the form BEFORE I set the SelectedIndex, like this: 此外,如果我在设置SelectedIndex之前将控件添加到表单中也没有问题,如下所示:

ComboBox comboBox1 = new ComboBox();
this.Controls.Add(comboBox1);
comboBox1.DataSource = comboBoxList;
comboBox1.SelectedIndex = 1;

Can anyone explain why I can't set the selected index from a datasource until after the control is added to the form? 任何人都可以解释为什么我不能从数据源设置选定的索引,直到控件添加到窗体后?

My understanding is that the databinding is handled by the bindingcontext normaly this is the parent forms bindingcontext. 我的理解是数据绑定由bindingcontext处理,因为这是父表单bindingcontext。 So the datasource binding does not happen until you add the comboBox to the form. 因此,在将comboBox添加到表单之前,不会发生数据源绑定。 You can also get this to work if you set the comboBox's bindingcontext to the forms binding context. 如果将comboBox的bindingcontext设置为窗体绑定上下文,也可以使其工作。

comboBox1.BindingContext = this.BindingContext;
comboBox1.DataSource = comboBoxList;
comboBox1.SelectedIndex = 1;
this.Controls.Add(comboBox1);

BindingContext Class BindingContext类

What is BindingContext 什么是BindingContext

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 InvalidArgument =值&#39;0&#39;对&#39;SelectedIndex&#39;无效 - InvalidArgument=Value of '0' is not valid for 'SelectedIndex' InvalidArgument =值&#39;5&#39;对于&#39;SelectedIndex&#39;无效 - InvalidArgument=Value of '5' is not valid for 'SelectedIndex' InvalidArgument=“0”的值对“SelectedIndex”无效。 参数名称:SelectedIndex - InvalidArgument=Value of '0' is not valid for 'SelectedIndex'. Parameter name: SelectedIndex C# InvalidArgument = '2' 的值对于 'index' 无效 - C# InvalidArgument = Value of '2' is not valid for 'index' System.ArgumentOutOfRangeException:InvalidArgument =值“ 0”对于“ SelectedIndex”无效 - System.ArgumentOutOfRangeException: InvalidArgument=Value of '0' is not valid for 'SelectedIndex' c#InvalidArgument =值&#39;-1&#39;对&#39;索引&#39;无效 - c# InvalidArgument=Value of '-1' is not valid for 'index' C#-数据绑定组合框始终为.selectedItem或.selectedValue返回空值 - C# - Databound Combobox always returns null value for .selectedItem or .selectedValue invalidargument =值&#39;8&#39;对于&#39;索引&#39;无效 - invalidargument=value of '8' is not valid for 'index' InvalidArgument =值&#39;4&#39;对于&#39;索引&#39;无效 - InvalidArgument=Value of '4' is not valid for 'index' InvalidArgument =值&#39;1&#39;对&#39;索引&#39;无效 - InvalidArgument=Value of '1' is not valid for 'index'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM