C# InvalidArgument=“0”的值对“SelectedIndex”无效。参数名称:SelectedIndex
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12839444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
InvalidArgument=Value of '0' is not valid for 'SelectedIndex'. Parameter name: SelectedIndex
提问by Mr_Green
I am getting the above error when i am trying this code. I tried giving just my codebut no use. (It was default)
尝试此代码时出现上述错误。我尝试只提供我的代码但没有用。(这是默认的)
The error is in cmbProduct_SelectedIndexChangedevent.
错误在cmbProduct_SelectedIndexChanged事件中。
cmbProduct --> combobox
cmbBrand --> combobox
Global
全球的
DataSet dsUpdate = new DataSet();
Form_load
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.
请帮助
提前致谢。
采纳答案by Danilo Vulovi?
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. Try to find following line:
从 Form1.Designer.cs 中删除 SelectedIndexChanged 事件初始化。尝试找到以下行:
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;
回答by Rashad Annara
You can also try this. Before setting combobox DataSource set its BindingContext
你也可以试试这个。在设置组合框 DataSource 之前设置其 BindingContext
cmbProduct.BindingContext = this.BindingContext;
回答by Think Big
i had same error. i think this error have a some reasons.
so my error is related to "set DataSourcein 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));
回答by JGFMK
If you have this issue:
如果你有这个问题:
- use the Form_Activatedevent handler to control setting the indexes.
- 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.
- I also had a boolean, firstFormActivation, when called the SetIndex the one time only..
- You can incidentally use this method for Focus() too, so first field in a Form gets focus when dynamically added.
- 使用Form_Activated事件处理程序来控制设置索引。
- 对我来说,我添加了一系列动态生成的 ComboBox。
- 我列出了我想要使用 SetIndex=0 的列表,然后在这个处理程序中遍历它们。
- 我也有一个布尔值 firstFormActivation,当只调用一次 SetIndex 时..
- 您也可以顺便将这个方法用于 Focus(),因此表单中的第一个字段在动态添加时会获得焦点。
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;
}
}
}

