I have a nasty exception that seems to occure deep inside the DataGridView Code.
I have a class that inherits from BindingList which is the DataSource of a BindingSource which is the DataSoure of a DataGridView.
Under some strange circumstances I get an exception during the overridden OnListChanged() method of my class:
protected override void OnListChanged(ListChangedEventArgs e)
{
base.OnListChanged(e); // <-- ArgumentOutOfRangeException
// ...Parametername: rowIndex
}
the stacktrace looks like this:
bei System.Windows.Forms.DataGridView.GetCellDisplayRectangle(Int32 columnIndex, Int32 rowIndex, Boolean cutOverflow)
bei System.Windows.Forms.DataGridView.GetCellAdjustedDisplayRectangle(Int32 columnIndex, Int32 rowIndex, Boolean cutOverflow)
bei System.Windows.Forms.DataGridView.InvalidateCellPrivate(Int32 columnIndex, Int32 rowIndex)
bei System.Windows.Forms.DataGridView.OnCellCommonChange(Int32 columnIndex, Int32 rowIndex)
bei System.Windows.Forms.DataGridView.DataGridViewDataConnection.ProcessListChanged(ListChangedEventArgs e)
bei System.Windows.Forms.DataGridView.DataGridViewDataConnection.currencyManager_ListChanged(Object sender, ListChangedEventArgs e)
bei System.Windows.Forms.CurrencyManager.OnListChanged(ListChangedEventArgs e)
bei System.Windows.Forms.CurrencyManager.List_ListChanged(Object sender, ListChangedEventArgs e)
bei System.Windows.Forms.BindingSource.OnListChanged(ListChangedEventArgs e)
bei System.Windows.Forms.BindingSource.InnerList_ListChanged(Object sender, ListChangedEventArgs e)
bei System.ComponentModel.ListChangedEventHandler.Invoke(Object sender, ListChangedEventArgs e)
bei System.ComponentModel.BindingList`1.OnListChanged(ListChangedEventArgs e)
bei My.Own.BindingList.OnListChanged(ListChangedEventArgs e)
Well, I could just add a try\catch exception around this, but I am curious why this happens at all.
Someone told me one time, I could use the mighty power of reflections and System.Diagnostics.StackTrace to get the StackFrame which causes the exception (That works so far) and inspect the parameters (I have no clue how to do this) which would help me, beause if I know the value from rowindex / columnindex I could track down the exception.
Can anybody tell me, if possible at all, to get the Parameters from the Exception?
Thanks in advance!
Update:
The problem seems to be related to some threading issues and has nothing to do with the rowIndex. BindingList is the datasource of an DataGridView. If I add an Element to the list the OnListChanged event fires, which causes the dataGridView to load the databound properties from the new instance of T. In the getter of one property I had some code that changed another property which caused the OnPropertyChanged Event of the T instace to be fired.
Just one simple lock(this) in the BindingList Add method like here: http://stackoverflow.com/questions/148587/has-anyone-written-a-thread-safe-bindinglistt solved the problem for me. Hard to debug ;(