0

I've got a DataTable bound to a WPF (.NET 3.5, WPFToolkit.dll) DataGrid. This is inside a very large client-server application. The server can asynchronously send data to the client "engine"; this data is sent in the form of properties which are set onto my control by the engine.

So, as an example of what I mean, I have a class MyFunkyDataGrid : DataGrid. MyFunkyDataGrid has a DependencyProperty called MyFunkyData which is of type Dictionary<string, Object>. Whenever MyFunkyData is changed on the server, it is immediately synchronized on the client, and set onto MyFunkyDataGrid. When MyFunkyData changes, I rip through the dictionary and set the data onto my grid's DataTable.

Of course, when this happens, I first need to clear the DataTable of its current data. So I call dataTable.Clear(). Normally this works a treat. BUT if I am currently editing a cell in MyFunkyDataGrid when the dataTable is cleared I get:

System.ExecutionEngineException was unhandled
  Message=Exception of type 'System.ExecutionEngineException' was thrown.
  InnerException: 

which crashes my app. I've tried calling CancelEdit() on MyFunkyDataGrid, I've tried calling it on the DataRow itself. I've tried calling RejectChanges() on the DataTable before I call Clear(). No dice.

Has anyone ever seen something like this before?

4

1 回答 1

0

我不确定为什么即使在清除数据表之前调用 datagrid.CancelEdit() 也会引发异常。但是在清除数据网格之前可以采取另外两个预防措施,这可能会避免异常:

(1)您可以将selecteditem设置为datagrid的null

(2)可以将datagrid的ItemSource设置为null,而不是datatable,然后清除datatable。

于 2012-08-13T12:41:49.627 回答