Copy DataGridView contents and style/formatting

M

more

I'm trying to copy cells from a datagridview that when copied, will
most likely be pasted by the user either into Word or an email(Outlook
using Word as the editor). I can get teh data to copy and paste
correctly as far as getting the actual information that needs to be
sent, but it's formatted all goofy. I'd like to be able to have it
treated like Access or Excel(with cell styles conigured) where it's
pasted back in a nicely formatted table with cell borders and bolder
headers, etc. As far as I can tell there are no built in methods to
handle this. Any thoughts?
 
A

andy

I'm trying to copy cells from a datagridview that when copied, will
most likely be pasted by the user either into Word or an email(Outlook
using Word as the editor).  I can get teh data to copy and paste
correctly as far as getting the actual information that needs to be
sent, but it's formatted all goofy.  I'd like to be able to have it
treated like Access or Excel(with cell styles conigured) where it's
pasted back in a nicely formatted table with cell borders and bolder
headers, etc.  As far as I can tell there are no built in methods to
handle this.  Any thoughts?

You're correct.
There are no built in methods.

You'd have to work out the selected columns and get their headings.
I think the clipboard data is tab delimited.
Parse it and manipulate to taste.

Not a trivial exercise.

When the users then decide they want to paste into excel you may have
a complication.

In the past I've provided excel "reports" via xslt. They were very
popular. Users like excel and the sort who are emailing data around
often know how to manipulate data in it, Mind you, these were asp.net
projects.
 
M

more

You're correct.
There are no built in methods.

You'd have to work out the selected columns and get their headings.
I think the clipboard data is tab delimited.
Parse it and manipulate to taste.

Not a trivial exercise.

When the users then decide they want to paste into excel you may have
a complication.

In the past I've provided excel "reports" via xslt. They were very
popular. Users like excel and the sort who are emailing data around
often know how to manipulate data in it, Mind you, these were asp.net
projects.

Actually I made a little headway on it. I had been simply calling

Clipboard.SetDataObject(dgOrders.GetClipboardContent(), true);

Where dgOrders is my DataGridView. In the DataObject
GetClipboardContent returns I was able to extract my content formated
in HTML using a fun combination of memory streams and byte arrays.
From there I can manually apply styles I need using HTML, but now my
problem comes in where it jsut pastes the code, rather than the
formatted table. I suspect it has something to do with the way i'm
sticking it in the clipboard but i'm not sure how to proceed from
here. Google hasn't really turned up much for me.
 
R

reasonman

Actually I made a little headway on it. I had been simply calling

Clipboard.SetDataObject(dgOrders.GetClipboardContent(), true);

Where dgOrders is my DataGridView. In the DataObject
GetClipboardContent returns I was able to extract my content formated
in HTML using a fun combination of memory streams and byte arrays.
From there I can manually apply styles I need using HTML, but now my
problem comes in where it jsut pastes the code, rather than the
formatted table. I suspect it has something to do with the way i'm
sticking it in the clipboard but i'm not sure how to proceed from
here. Google hasn't really turned up much for me.

Alright I figured it out. Turns out to put html formatted data into
the clipboard you need to prepend some data to it. I found a method
here:

http://blog.tcx.be/2005/08/copying-html-fragment-to-clipboard.html

That makes it easy to do, making a couple minor changes to allow me to
format it the way I'd like it. Thanks for the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top