“0”InvalidArgument = Value是无效的“的SelectedIndex” [英] InvalidArgument=Value of '0' is not valid for 'SelectedIndex'

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

问题描述

在.NET 4.0

我工作的Windows窗体应用程序。当我将数据绑定到的BindingSource (到一个组合框绑定),我得到下面的异常。注:我明白了只有当我做调试器停在抛出异常,无论是未处理或处理。因此,将抛出异常的地方 - 但尽管如此,我不知道是否正常抛出

  发生

ArgumentOutOfRangeException   0InvalidArgument = Value是无效的SelectedIndex。   参数名:SelectedIndex的

我不设置的SelectedIndex 属性。我的code如下所示。 的myData 是的的IList 实体(列表在运行时)

  myBindingSource.DataSource = myData的;
 

我想不出什么我做错了。此外,调用堆栈是混淆了我一个位(看到它下面)。 Windows窗体框架似乎在组合框中,这将导致异常被设定的SelectedIndex 。有谁知道摆脱这样的一种方式?

干杯 马蒂亚斯

  System.Windows.Forms.dll的!System.Windows.Forms.ComboBox.SelectedIndex.set(int值)+ 0x233字节
System.Windows.Forms.dll中!System.Windows.Forms.CurrencyManager.OnPositionChanged(System.EventArgs发送)+ 0x3E的字节
System.Windows.Forms.dll中!System.Windows.Forms.CurrencyManager.ChangeRecordState(INT在newPosition,布尔核实,布尔endCurrentEdit,布尔firePositionChange,布尔pullData)+ 0x1bd字节
System.Windows.Forms.dll中!System.Windows.Forms.CurrencyManager.List_ListChanged(对象发件人,System.ComponentModel.ListChangedEventArgs E)+ 0x75c字节
System.Windows.Forms.dll中!System.Windows.Forms.BindingSource.ResetBindings(布尔metadataChanged)+ 0x3E的字节
System.Windows.Forms.dll中!System.Windows.Forms.BindingSource.SetList(System.Collections.IList列表,布尔metaDataChanged,布尔applySortAndFilter)+ 0x22c字节
System.Windows.Forms.dll中!System.Windows.Forms.BindingSource.DataSource.set(对象的值)+ 0X47字节
(我的方法)
 

解决方案

当你问调试器停止例外,它会做到这一点,无论他们是否将被处理或没有。这导致了像你看到一个场景:
调试器停在一个例外,迷惑你,虽然该异常是完全有效的,似乎被周围code可以预料的,因为它处理异常而不死。

要总结和回答你的问题:
不是所有的异常调试器停止时是你正在做的事情错了一个指标,或出现在code的一个问题。

更新(学分去标记):
你可以告诉调试器只捉住你的异常,如果启用该选项只是我的code。

I'm working on a Windows Forms application in .NET 4.0. As I am binding data to a BindingSource (to which a ComboBox is bound), I get the following exception. Note: I get it only if I make the debugger stop at exceptions being thrown, either unhandled or handled. Thus, the exception is caught somewhere - but nevertheless I'm not sure whether it is OK to be thrown.

ArgumentOutOfRangeException occurred InvalidArgument=Value of '0' is not valid for 'SelectedIndex'. Parameter name: SelectedIndex

I'm not setting the SelectedIndex property. My code is shown below. myData is an IList of entities (List at runtime):

myBindingSource.DataSource = myData;

I can't figure out what I'm doing wrong. Moreover, the Call Stack is confusing me a bit (see it below). The Windows Forms framework seems to be setting SelectedIndex on the combo box, which causes the exception. Does anybody know a way of getting rid of this?

Cheers Matthias

System.Windows.Forms.dll!System.Windows.Forms.ComboBox.SelectedIndex.set(int value) + 0x233 bytes   
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.OnPositionChanged(System.EventArgs e) + 0x3e bytes    
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.ChangeRecordState(int newPosition, bool validating, bool endCurrentEdit, bool firePositionChange, bool pullData) + 0x1bd bytes    
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.List_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e) + 0x75c bytes   
System.Windows.Forms.dll!System.Windows.Forms.BindingSource.ResetBindings(bool metadataChanged) + 0x3e bytes    
System.Windows.Forms.dll!System.Windows.Forms.BindingSource.SetList(System.Collections.IList list, bool metaDataChanged, bool applySortAndFilter) + 0x22c bytes 
System.Windows.Forms.dll!System.Windows.Forms.BindingSource.DataSource.set(object value) + 0x47 bytes   
(my method)

解决方案

When you ask the debugger to stop on Exceptions, it will do so, regardless whether they will be handled or not. This leads to scenarios like the one you observed:
The debugger stops at an exception and confuses you, although the exception is perfectly valid and seems to be expected by the surrounding code, because it handles the exception without dying.

To sum up and answer your question:
Not all exceptions the debugger stops at are an indicator that you are doing something wrong or that there is a problem in your code.

Update (credits go to Mark):
You can tell the debugger to only catch your exceptions, if you enable the option "Just my code".

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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆