Reputation: 718
UPDATE
It appears this may be something else. There is a BindingList of accounts bound to a ListBox. When the user selects an account, their billing periods show up in the ComboBox (after which they can view the bills for each period). Before I even attempt to add an item to an empty list bound to the ComboBox, if I switch from an account that has loaded a pre-existing billing period, to one with no billing period, and then try to switch back, the same exception is thrown. The information below is for the scenario where I would switch to an account that does not have a pre-existing billing period, and attempt to add a new one. I've added some more code to show the binding for the ListBox, if it helps.
END UPDATE
I'm running into what seems to be a simple issue but I cannot figure out how to proceed. I have a ComboBox
data bound to a BindingList
(encapsulated by a BindingSource
), containing billing period objects. The objects contain their own BindingLists
, the contents of these lists are displayed in a grid (as bills). This way when the user selects a billing period, the grid reflects the correct bills for the period.
Here is the binding code:
BindingSource bs = new BindingSource(Controller, "Accounts");
listAccounts.DataSource = bs;
listAccounts.DisplayMember = "Name";
BindingSource source = new BindingSource(bs, "Periods");
comboPeriod.DataSource = source;
comboPeriod.DisplayMember = "DisplayPeriod";
BindingSource src = new BindingSource(source, "Bills");
gridPaymentSchedule.DataSource = src;
Not all accounts will have a pre-existing billing period, so the ComboBox will bind to an empty list. When I choose to add a billing period to these empty lists at a later point via Periods.Add(period)
, I get the following exception:
Exception thrown: 'System.ArgumentOutOfRangeException' in System.Windows.Forms.dll
Additional information: InvalidArgument=Value of '0' is not valid for 'SelectedIndex'.
So I did some searching on the web and I found that by adding an item to the data source of a ComboBox, even if it is empty it fires the SelectedIndexChanged event, however when it is empty a SelectedIndex of '0' is invalid because that index refers to the first item (which makes sense considering the exception text).
My problem then is this: I want to keep my binding behavior to maintain the ComboBox item changing the displayed bills. I can't think of a way of doing this or haven't figured one out where it doesn't trip over an initally empty data source. Ideally the behavior I am going for is that the ComboBox doesn't bind unless it's underlying data source is non-empty (to avoid this error).
The only other thing I can think of is to manually manage the ComboBox and data grid without binding but this will be a major pain. Is there no other way? There has to be!
Upvotes: 0
Views: 757
Reputation: 16779
My problem was similar but solution was different. I got this very same exception, with ComboBox.DataSource
and BindingList
, but only when debugging, and exceptions did not really stop the execution. When I just run the app there was no exception. I then enabled "Enable Just My Code" option in Tools > Options > Debugging > General and exceptions were gone.
Upvotes: 3
Reputation: 718
After having a friend look at the code, we tried the same code base on a different machine. It worked fine, with no changes. We tried a bunch of "fixes" on the original machine and finally it worked with some hacky fixes... Turns out we didn't need the hacky fixes at all, it was a caching issue with the build files.
Remember kids, REBUILD your project if you're having issues.
So, to note: there was no issue at all, my build files were out of date and were causing erroneous behavior.
Upvotes: 0