Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF VB.NET
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 19-Nov-12 14:39pm
Edited 19-Nov-12 17:26pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v4
Comments
Vijay hit at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
 

    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
    
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,649
1 Sergey Alexandrovich Kryukov 6,270
2 CPallini 5,190
3 George Jonsson 3,574
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 21 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100