Question Copying Datagridview to Clipboard formatted for Excel

tamarnator

New member
Joined
Dec 16, 2013
Messages
1
Programming Experience
10+
I have an application which generates a list of X-Y coordinates on a curve, and captures them in a datagridview. I have customers that want to be able to copy this data into an Excel spreadsheet. I am using a ContextMenu to allow the user to capture data, and copy it to the clipboard using Clipboard.SetDataObject(). When I paste to Excel, I have found that I lose formatting information significant to the user, such as cell background color. I discovered that "Paste Special..." -> Text copies the data in without the loss of cell formatting. What I am wondering is whether it is possible to copy data to the clipboard in such a way that a simple "Paste" can accomplish the same thing as "Paste Special..." -> Text, and allow formatting to be preserved. I tried messing around with DataFormats.Text wondering if I copied the data in Text format, if it might preserve formatting, but I have been unsuccessful thus far.

Here is the code I have working:

Private Sub MenuuItemCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemCopy.Click
Dim objData As DataObject = dgCurve.GetClipboardContent

If objData isNot Nothing Then
Clipboard.SetDataObject(objData, True)
End if
End Sub

Thanks for any suggestions!
 
Back
Top