I have run into a strange issue. Apparently, it has been around for a time. I only noticed it because I changed a setting so that my code paused when a CLR exception was thrown. Without this set, the program works fine, and I don't believe it's really an issue, but it's a bit puzzling.

I have a datatable that is set as the datasource for a listbox. This datatable starts out empty. When the user does one of a variety of things, something gets added to the datatable and it shows up in the listbox. That is what is working. However, when looking for exceptions, I found that there was an exception on this line:

mBackingStore.Tables("MWInfo").Rows.Add(ndr)

That is the line that adds a new row into the datatable. The table was empty, the table will now have one row. This causes an ArgumentOutOfRangeException with the additional infoo that 0 is not valid for SelectedIndex.

Since a datatable has no selectedindex, I assume this is refering to the listbox, but 0 IS a valid value for a listbox that has anything in it, and this one is at least ABOUT to have something in it as a result of something being added to the datatable.

So, that makes it look like the selectedindex is being advanced from -1 to 0 before the item is actually in the listbox. In other words, the act of adding to the datatable will add to the listbox, but the selectedindex of the listbox is advancing before the item is actually there.

Is that right? And secondarily, is this something that I can, or should, do anything about? Unhandled exceptions can be costly. In this case, the exception is handled internally to the CLR, because the actual behavior is correct, the item is added to the listbox and selected (I don't care that it is selected, but it is), so it works fine, there's just an unexceptional exception taking place.