Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am writing an application in which when user select rows in Datagrid and press Ctrl C, it has to get the clip board content. When Ctrl V is pressed it has to paste.

I am setting mode as
myDataGrid.ClipBoardCopyMode = Windows.Controls.DataGridCopyMode.ExcludeHeader

after this I want to get the clipboard content as in the windows form where we use

myDataGridView.GetClipboardContent() method to get the content.


Is there any method in WPF similar to GetClipboardContent() of Windows Form.


Appreciate any suggestions,

Vijay
Posted
Updated 19-Nov-12 17:26pm
v2

You need to use this: http://msdn.microsoft.com/en-us/library/system.windows.clipboard.aspx[^].

The method you want is GetData or GetDataObject.

Probably, your problem is working with the custom data format. In this clipboard class, this functionality is predefined via the special data format expressed by means of the class DataFormats and its public field DataFormats.Serializable:
http://msdn.microsoft.com/en-us/library/system.windows.dataformats.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.dataformats.serializable.aspx[^].

The use of this format is based on the assumption of using the class DataObject implementing the interface IDataObject:
http://msdn.microsoft.com/en-us/library/system.windows.dataobject.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.idataobject.aspx[^].

The background of it is pretty simple and interesting. As the low-level of the Clipboard is some serialized raw byte data (how else? Clipboard is system-wide), the mechanism is based on serialization:
http://en.wikipedia.org/wiki/Serialization[^],
http://en.wikipedia.org/wiki/Serialization#.NET_Framework[^].
http://msdn.microsoft.com/en-us/library/vstudio/ms233843.aspx[^].

Custom Clipboard types are registered with some string atom, unique in the scope of system which should be recognized by the code putting data in Clipboard and reading it. It's enough that both parts used identical serialization mechanism, not matter what kind. As soon as those to parts of code share the same mechanism, everything works; and for the applications not involved in using this custom type, this is some unrecognized format they are not subscribed to.

—SA
 
Share this answer
 
v4
Comments
Vijay hit 21-Nov-12 22:12pm    
Hi Sergey,

Thanks for the info.
I used GetData method suggested by you to get the clipboard content.
It is working fine.

Regards,
Vijay
Sergey Alexandrovich Kryukov 21-Nov-12 22:32pm    
This is great! This is a pleasure to help people who can get it and actually make things work!
Good luck, call again.
--SA
To get the DataGrid content to clipboard we should use the Clipboard GetData method.

Below example shows how to copy All the cells of DataGrid "myDataGrid" to textbox.
It will copy the ColumnName of DatGrid as IncludeHeader is selected for copy mode.


VB
Me.myDataGrid.SelectAllCells()
Me.myDataGrid.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader
ApplicationCommands.Copy.Execute(Nothing, Me.myDataGrid)
Dim tempString as String = Clipboard.GetData(DataFormats.CommaSeperatedValue)

Me.myTextBox.text = tempString
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900