I create a grid which has only one column with about 800K rows.
When I copy all the data, it launch a out of memory exception.
The grid writes to the clipboard in multiple formats including CSV, Text, Html and its own internal format. In this case the Html format results in a string of approximalely 123,000,000 characters and .net cannot generate a string of that length because it doesn't have a large enough contiguous block of memory. I would probably recommend not letting the user try to copy all the data to the clipboard (e.g. handle the ExecutingCommand and setting e.Cancel to true if there are selected fields) if you have such a large data source.
private void grid_ExecutingCommand(object sender, Infragistics.Windows.Controls.Events.ExecutingCommandEventArgs e) { if (e.Command == DataPresenterCommands.Copy || e.Command == DataPresenterCommands.Cut) { DataPresenterBase dp = sender as DataPresenterBase; if (dp.SelectedItems.Fields.Count > 0) { e.Cancel = true; return; } } }
if (dp.SelectedItems.Fields.Count > 0) { e.Cancel = true; return; } } }
If you must do this then perhaps you can exclude some formats (e.g. Html). Note it still may be possible to exceed the available memory. You can do this by handling the DataObject.SettingData attached event (e.g. <igDP:XamDataGrid x:Name="grid" DataObject.SettingData="grid_SettingData" ).
private void grid_SettingData(object sender, DataObjectSettingDataEventArgs e) { if (e.Format == DataFormats.Html) { e.CancelCommand(); } }
Thanks for answer so quickly.
I try to cancel html data. But it looks I have similar problem with other format.
The following is my callstack:
I've canceled csv format:
private void grid_SettingData(object sender, RoutedEventArgs e) { DataObjectSettingDataEventArgs dbsd = e as DataObjectSettingDataEventArgs; if (e != null && (dbsd.Format == DataFormats.Html || dbsd.Format == DataFormats.CommaSeparatedValue)) { dbsd.CancelCommand(); } }
Is there anyother format I should cancel?
You could try cancelling the grid's internal format which would have a value of "Infragistics.Windows.DataPresenter.ClipboardData" since you won't be pasting this into a grid. It is still possible that when the application is used that a large enough block of memory won't be available though so its probably best to not allow copying of all the rows' data.