Reputation: 9576
Some data is displayed in a datagridview (Winform). I have a requirement to mail the table represented in the datagridview in a similar format (Rows, Columns etc). What is the easiest was I can do it? How to convert that to html table with Column headers and preserving the column width.
Upvotes: 0
Views: 6380
Reputation: 20757
Quick Google Search yielded this: How to send DataGridView contents by email. It covers converting the DGV to an HTML table and mailing it.
Upvotes: 1
Reputation: 2072
I think if you search you'd find this on the web
If you don't ...
Do the following
1. Create a stringbuilder, wrap it with htmlTextWriter
2. Create a htmltable, add a header row,
3. Iterate through the gridheaders and add cells, write the width in attributes, write other stuff like font, fontsize, forecolor, backcolor etc if you want
4. Next iterate through the grid rows, add a row each time to table, add cells to row for each grid cells
5. Call the render method passing the html writer
6. Take the string builder out and you have a html table layout output.
Upvotes: 1
Reputation: 27561
I don't know if there is any framework level conversion that can take place. But it should be a pretty simple matter to iterate through the grids rows, and cells, and recreate it in HTML with the Width's in place.
Written off the cuff, please excuse its incompleteness and any simple mistakes, but I think you can get the basic idea here.
StringBuilder sb = new SringBuilder();
sb.AppendLine("<table>");
sb.AppendLine(" <tr>");
foreach(DataGridViewColumn column in grid.Columns)
{
sb.AppendLine(" <td>" + column.HeaderText + "</td>");
}
sb.AppendLine(" </tr>");
foreach(DataGridViewRow row in grid.Rows)
{
sb.AppendLine(" <tr>");
foreach(DataGridViewCell cell in row.Cells)
{
sb.AppendLine(" <td width=\"" + cell.Width + "px\">" + cell.Value + "</td>");
}
sb.AppendLine(" </tr>");
}
sb.AppendLine("</table>");
Upvotes: 2