本文介绍了“0”InvalidArgument = Value是无效“的SelectedIndex”。参数名:的SelectedIndex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到上面的错误,当我试图这样code。我试着给 只是我的code 但没有用。 (这是默认)

I am getting the above error when i am trying this code. I tried giving just my code but no use. (It was default)

这是我的XML文件

该错误是在 cmbProduct_SelectedIndexChanged 事件。

        cmbProduct --> combobox
        cmbBrand   --> combobox

全球

        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

        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.

推荐答案

但问题是:

当你启动应用程序时,你不必在cmbBrand项目,但cmbProduct火灾的SelectedIndexChanged。

when you start application, you do not have items in cmbBrand, but cmbProduct fires SelectedIndexChanged.

试试这个:

从Form1.Designer.cs删除SelectedIndexChanged事件初始化。试图找到以下行:

remove SelectedIndexChanged event initialization from Form1.Designer.cs. Try to find following line:

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

在此之后,当你填充数据集从XML文件中的数据,初始化SelectedIndexChanged事件:

After that, when you populate DataSet with data from xml file, initialize SelectedIndexChanged event:

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;

这篇关于“0”InvalidArgument = Value是无效“的SelectedIndex”。参数名:的SelectedIndex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 04:49