[英]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:如果你有这个问题:
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.