简体   繁体   English

InvalidArgument =值'18'对'SelectedIndex'无效参数名称:SelectedIndex

[英]InvalidArgument=Value '18' is invalid to 'SelectedIndex' Parameter name: SelectedIndex

I have a Button where I am able to save data into the database. 我有一个Button ,可以将数据保存到数据库中。 To do that I will get the information on Combobox and on a Textbox . 为此,我将在ComboboxTextbox上获取信息。 I got a collection of items iniside of the Combobox and cannot be changed at the moment. 我得到了项目iniside的的集合Combobox ,并不能在瞬间不会改变。

Right now, I have 17 items and every time I save something it will pull to the next item using cmbID.SelectedIndex += 1; 现在,我有17个项目,每次保存时,它将使用cmbID.SelectedIndex += 1;拉到下一个项目cmbID.SelectedIndex += 1; , but every time I pull to the last item from Combobox it will give me an error : ,但是每次我从Combobox拉到最后一项时,都会给我一个错误:

InvalidArgument=Value '18' is invalid to 'SelectedIndex' Parameter name: SelectedIndex InvalidArgument =值'18'对'SelectedIndex'无效参数名称:SelectedIndex

To solve it I've tried to use an if statement: 为了解决这个问题,我尝试使用if语句:

if (cmbID.SelectedIndex >= 18)
{
    cmbID.SelectedIndex = 1;
}

But this is not working, basically if the Combobox reach '18' it should go back to the SelectedIndex choosen by me. 但这是行不通的,基本上,如果组合Combobox达到“ 18”,它应该回到我选择的SelectedIndex

Do you guys have any idea I can solve this problem? 你们有什么主意可以解决这个问题吗?

You can't have index more than (item count -1) for the combobox, so 组合框的索引不能超过(项目计数-1),因此

if(cmbID.SelectedIndex == (cmbID.Items.Count - 1))
{
    cmbID.SelectedIndex =1;
}else
{
    cmbID.SelectedIndex += 1;
}

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

相关问题 InvalidArgument=“0”的值对“SelectedIndex”无效。 参数名称:SelectedIndex - InvalidArgument=Value of '0' is not valid for 'SelectedIndex'. Parameter name: SelectedIndex InvalidArgument =值'0'对'SelectedIndex'无效 - InvalidArgument=Value of '0' is not valid for 'SelectedIndex' InvalidArgument =值'5'对于'SelectedIndex'无效 - InvalidArgument=Value of '5' is not valid for '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' 带有SelectedIndex和Value的Listbox Bug? - Listbox Bug with SelectedIndex and Value? SelectedIndex没有插入正确的值 - SelectedIndex not inserting correct value InvalidArgument =值'3'对于'索引'无效。 参数名称:索引 - InvalidArgument=Value of'3' is not valid for 'index'. parameter name: index InvalidArgument='-1' 的值对 'index' 无效。 参数名称:索引 - InvalidArgument=Value of '-1' is not valid for 'index'. Parameter name: index ListView的SelectedIndex错误值 - ListView's SelectedIndex wrong value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM