Found the answer at this link. Thanks to the author and to the person who pointed me to it. The data table or datagridview has to be represented as a html table. The class "Test" is the key to this code.
http://blog.tcx.be/2005/08/copying-html-fragment-to-clipboard.html[
^]
The example given by the author for copying and formatting a data table does not work although it has the right idea.
Here is code I wrote and tested for copying and formatting a datagridview. Take note that I did some formatting by coloring the cells green.
StringBuilder html = new StringBuilder();
html.Append("<table>");
for (int i = 0; i <= dataGridView1.RowCount - 1; i++)
{
html.Append("<tr>");
for (int j = 0; j <= dataGridView1.ColumnCount - 1; j++)
{
DataGridViewCell cell = dataGridView1[j, i];
html.AppendFormat("<td bgcolor=green>{0}</td>", cell.Value);
}
html.Append("</tr>");
}
html.Append("</table>");
Test.CopyHtmlToClipBoard(html.ToString());