Copying from DataGrid to Excel

Apr 1, 2011 at 8:12 AM
Edited Apr 1, 2011 at 8:53 AM


As per my knowledge so far, I understand that if I try to copy data from DataGrid in the view to an Excel sheet, it works by default. The sample applications I have seen just need to bind the data and after the user can edit or just copy data from DataGrid and paste it to an excel sheet or notepad. The only case which requires some amount of effort in terms of coding is while trying to paste data to DataGrid from an Excel sheet or a text document.

We have to be able to copy data from DataGrid to an Excel sheet, but it is not working. When the user selects a few cells in the grid and does a Ctrl + C to copy and tries to paste, nothing gets pasted except new lines. On investigating, I relaized that on doing a Ctrl + C(Copy command), data doesn't get copied to Clipboard.

I have tried few things like :

1. Making data model implement IEditableObject and ICloneable.

2. Allowing edit on DataView associated with the data table before data table is bound to data grid.

3. Trying to set data object in Clipboard through code.

4. Tried changing the way data is currently bound to the grid.

5. Tried allowing editing on DataGridCell.

6. Tried changing settings like IsReadOnly, CanUser*.

The view is un-editable by default, which I think should have been editable by default.

I am not sure if this is relevant, but when data is rendered, Output console shows errors like :

1. System.Windows.Data Error: 17 : Cannot get 'Item[]' value (type 'ValidationError') from '(Validation.Errors)' (type 'ReadOnlyObservableCollection`1'). BindingExpression:Path=(0)[0].ErrorContent; DataItem='DataGridRow' (Name=''); target element is 'Control' (Name=''); target property is 'DataContext' (type 'Object') ArgumentOutOfRangeException:'System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.

Parameter name: index'


2. System.Windows.Data Error: 40 : BindingExpression path error: 'Tag' property not found on 'object' ''Object' (HashCode=1993781)'. BindingExpression:Path=Tag; DataItem='Object' (HashCode=1993781); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

I was wondering if someone has any idea.