简体   繁体   English

InvalidArgument=“0”的值对“SelectedIndex”无效。 参数名称:SelectedIndex

[英]InvalidArgument=Value of '0' is not valid for 'SelectedIndex'. Parameter name: SelectedIndex

I am getting the above error when i am trying this code.尝试此代码时出现上述错误。 I tried giving just my code but no use.我试着 只给出我的代码但没有用。 (It was default) (这是默认的)

Here is my XML file这是我的 XML 文件

The error is in cmbProduct_SelectedIndexChanged event.错误出现在cmbProduct_SelectedIndexChanged事件中。

        cmbProduct --> combobox
        cmbBrand   --> combobox

Global全球的

        DataSet dsUpdate = new DataSet();

Form_load表单加载

        dsUpdate.ReadXml(@"...\..\stock.xml");
        cmbProduct.DataSource = dsUpdate.Tables[0]
            .DefaultView.ToTable(true, "productname");//.DefaultView;
        cmbProduct.DisplayMember = "productname";
        cmbProduct.SelectedIndex = 0;

cmbProduct_SelectedIndexChanged cmbProduct_SelectedIndexChanged

        cmbBrand.Items.Clear();
        foreach (DataRow Row in dsUpdate.Tables[0].Select("productname='" + cmbProduct.Text + "'"))
        {
            //cmbBrand.SelectedIndex = i;
            cmbBrand.Items.Add(Row["brandname"].ToString());
            //i++;
        } 
        cmbBrand.SelectedIndex = 0; /*ERROR*/  

Please help请帮忙
Thanks in Advance.提前致谢。

Problem is:问题是:

when you start application, you do not have items in cmbBrand, but cmbProduct fires SelectedIndexChanged.当您启动应用程序时,cmbBrand 中没有项目,但 cmbProduct 会触发 SelectedIndexChanged。

Try this:尝试这个:

remove SelectedIndexChanged event initialization from Form1.Designer.cs.从 Form1.Designer.cs 中删除 SelectedIndexChanged 事件初始化。 Try to find following line:尝试找到以下行:

this.cmbProduct.SelectedIndexChanged += new System.EventHandler(this.cmbProduct_SelectedIndexChanged);

After that, when you populate DataSet with data from xml file, initialize SelectedIndexChanged event:之后,当您使用 xml 文件中的数据填充 DataSet 时,初始化 SelectedIndexChanged 事件:

dsUpdate.ReadXml(@"...\..\stock.xml");
cmbProduct.DataSource = dsUpdate.Tables[0].DefaultView.ToTable(true, "productname");//.DefaultView;
cmbProduct.DisplayMember = "productname";
this.cmbProduct.SelectedIndexChanged += new System.EventHandler(this.cmbProduct_SelectedIndexChanged);
cmbProduct.SelectedIndex = 0;

i had same error.我有同样的错误。 i think this error have a some reasons.我认为这个错误有一些原因。 so my error is related to "set DataSource in another thread is not working"所以我的错误与“在另一个线程中设置DataSource不起作用”有关

example例子

//Run in another thread
myComboBox.DataSource = myDataSource; //not set

fix with修复

myComboBox.Invoke(new Action(() => myComboBox.DataSource = myDataSource));

You can also try this.你也可以试试这个。 Before setting combobox DataSource set its BindingContext在设置组合框 DataSource 之前设置它的 BindingContext

cmbProduct.BindingContext = this.BindingContext;

This will happen if you attempt to set the SelectedIndex while there is no valid datasource.如果您在没有有效数据源的情况下尝试设置 SelectedIndex,则会发生这种情况。 If you're resetting the default to 0, and occasionally changing the datasource list, you may see this.如果您将默认值重置为 0,并偶尔更改数据源列表,您可能会看到这一点。 You don't need to default to 0 if applying a new datasource, so simple check will avoid it happening:如果应用新的数据源,则不需要默认为 0,因此简单的检查将避免它发生:

if (comboBox.Datasource != null) comboBox.SelectedIndex = 0; if (comboBox.Datasource != null) comboBox.SelectedIndex = 0;

If you have this issue:如果你有这个问题:

  • use the Form_Activated event handler to control setting the indexes.使用Form_Activated事件处理程序来控制设置索引。
  • For me, I had a series of dynamically generated ComboBoxes I added to a Form.对我来说,我将一系列动态生成的组合框添加到表单中。
  • I made a list of the ones where I wanted to use SetIndex=0, then iterated through them in this handler.我列出了我想使用 SetIndex=0 的列表,然后在这个处理程序中遍历它们。
  • I also had a boolean, firstFormActivation, when called the SetIndex the one time only..当只调用一次 SetIndex 时,我也有一个布尔值 firstFormActivation。
  • You can incidentally use this method for Focus() too, so first field in a Form gets focus when dynamically added.您也可以顺便将此方法用于 Focus(),因此 Form 中的第一个字段在动态添加时会获得焦点。

Here is some code to illustrate the point:下面是一些代码来说明这一点:

    private readonly List<ComboBox> combosToSetIndexOn = new List<ComboBox>();
    private bool firstActivation = true;
    private Control firstWindowsControl = null;

    ...
    // Other code sets firstWindowsControl...

    private void DynamicForm_Activated(object sender, EventArgs e)
    {
        if (firstActivation)
        {
            firstActivation = false;
            bool fwcPresent = (firstWindowsControl != null);
            Console.WriteLine($"DynamicForm_Activated: firstWindowControl present: {fwcPresent}");
            if (fwcPresent)
            {
                firstWindowsControl.Focus();
            }

            if (combosToSetIndexOn.Count > 0)
            {
                foreach (ComboBox c in combosToSetIndexOn)
                {
                    Console.WriteLine($"DynamicForm_Activated: processing: {c.Name}");
                    c.SelectedIndex = 0;
                }
            }
        }

In my case the following was causing my problem在我的情况下,以下是我的问题

myComboBox.DataSource = myBindingSource
myBindingSource.DataSource = items.ToList()  // error

The following worked以下工作

myComboBox.DataSource = null;
myBindingSource.DataSource = items.ToList();
MyComboBox.DataSource = myBindingSource;

声明:本站的技术帖子网页,遵循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 =值&#39;18&#39;对&#39;SelectedIndex&#39;无效参数名称:SelectedIndex - InvalidArgument=Value '18' is invalid to 'SelectedIndex' Parameter name: SelectedIndex System.ArgumentOutOfRangeException:InvalidArgument =值“ 0”对于“ SelectedIndex”无效 - System.ArgumentOutOfRangeException: InvalidArgument=Value of '0' is not valid for 'SelectedIndex' c #databound ComboBox:InvalidArgument =值'1'对'SelectedIndex'无效 - c# databound ComboBox : InvalidArgument=Value of '1' is not valid for 'SelectedIndex' InvalidArgument =值&#39;3&#39;对于&#39;索引&#39;无效。 参数名称:索引 - InvalidArgument=Value of'3' is not valid for 'index'. parameter name: index InvalidArgument=&#39;-1&#39; 的值对 &#39;index&#39; 无效。 参数名称:索引 - InvalidArgument=Value of '-1' is not valid for 'index'. Parameter name: index InvalidArgument =值&#39;6&#39;对于索引无效。 参数名称:索引 - InvalidArgument=Value of '6' is not valid for index. Parameter name: index 带有SelectedIndex和Value的Listbox Bug? - Listbox Bug with SelectedIndex and Value? SelectedIndex没有插入正确的值 - SelectedIndex not inserting correct value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM