Why does DGV cause uncaught exception when removing item from DataSource?

China☆狼群 提交于 2020-03-03 07:33:30

问题


I have a WinForms application with a DataGridView that has its DataSource property set to a BindingSource (which in turn has its DataSource set to a BindingList).

Everything works just fine, until I come to remove items from the BindingSource in response to a user request, the code I use for this is:

try
{
    // Get the item currently selected by the DGV
    var fc = (FooType)FooBindingSource.Current;
    // Remove the item from the binding source
    FooBindingSource.Remove(fch);
}
catch (Exception fault)
{
    MessageBox.Show(fault.Message);
}

The Remove() causes an exception. However in does not seem to be raised directly in the line of execution from this code as the cathc here does not catch the exception. Instead, when I run the application and test this functionality I see the DGV default error dialog several times. Suprisingly though, the end result is still that the item does get correctly deleted!

I can solve this by un-binding the BindingSource from the DGV before the Remove() (using FooDataGridView.DataSource = null) and rebinding it afterwards. But this feels... hacky, like I am just skirting round the issue rather than fixing it because I don't understand what is happening to cause the issue.

I believe that some event is happening while the item is removed, like the grid is getting validated or something.

What is causing this and is there a better way of preventing the exception?

The error dialogs I see contain the following information, all are titled "DataGridView Default Error Dialog"

First:

The following exception occurred in the DataGridView:

System.IndexOutOfRangeException: Index 413 does not have a value.

at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)

at System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError(Int32 rowIndex)

To replace this default dialog please handle the DataError event.

Second:

The following exception occurred in the DataGridView:

System.IndexOutOfRangeException: Index 413 does not have a value.

at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)

at System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError(Int32 boundColumnIndex, Int32 columnIndex, Int32 rowIndex)

To replace this default dialog please handle the DataError event.

Third:

The following exception occurred in the DataGridView:

System.IndexOutOfRangeException: Index 413 does not have a value.

at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)

at System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError(Int32 rowIndex)

To replace this default dialog please handle the DataError event.

P.s: Just writing a quick DataError event handler know to look into this more but thought I'd get a head start here.


回答1:


Looking at the DGV DataError event, I can see that the event args Context property is set to "Display" for all three of the events. MSDN documents the meaning of display as

A data error occurred when displaying a cell that was populated by a data source. This value indicates that the value from the data source cannot be displayed by the cell, or a mapping that translates the value from the data source to the cell is missing.

Thus I think this may have something to do with the DGV attempting to keep the removed row selected when it updates itself during the removal. Seeing as I have other TextBox controls displaying the same data also based on the selection then this looks like a possible thorny sort of circular validating event stack...

To test this I tried programmatically changing the selected row to something else immediately before removing the item - which worked! I could use this method but then what row should I have the code select when a user removes the last item? More thorns and spaghetti... So with at least a little understanding of what might be happening I'm OK now with using the unbinding and rebinding method I mentioned.



来源:https://stackoverflow.com/questions/43504584/why-does-dgv-cause-uncaught-exception-when-removing-item-from-datasource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!