0

So using the answer found on this link:

Creating HTML from a DataTable using C#

I successfully converted by datatable to HTML and it shows as a table in my email...

I know there must be a place to put the style for adding grid lines around the cells but I for the life of me can't find the right spot. I have successfully added a border around the entire table however...

DataTable dt = new DataTable();
sdaGetValidation.Fill(dt);   

StringBuilder sb = new StringBuilder();
sb.AppendLine("<html>");
sb.AppendLine("\t" + "<body>");
sb.AppendLine("\t\t" + "<table>");
sb.Append("<table border='1px' solid line black cellpadding='5' cellspacing='0' ");
sb.Append("style='border: solid 1px Silver; font-size: x-small;'>");

sb.Append("\t\t" + "<tr>");

foreach (DataColumn dc in dt.Columns)
{
    sb.AppendFormat("<td>{0}</td>", dc.ColumnName);
}

sb.AppendLine("<tr>");

foreach (DataRow dr in dt.Rows)
{
    sb.Append("\t\t\t" + "<tr>");

    foreach (DataColumn dc in dt.Columns)
    {
        string cellValue = dr[dc] != null ? dr[dc].ToString() : "";
        sb.AppendFormat("<td>{0}</td>", cellValue);
    }

    sb.AppendLine("</tr>");
}

sb.AppendLine("\t\t\t" + "</table>");
sb.AppendLine("\t" + "</body>");
sb.AppendLine("</html>");

MessageBox.Show(sb.ToString());

I am not sure if there was a way to rehash the old answer so I apologize if there is a way to do so...

Not much of an HTML guy so I feel like this should be an easy win for someone who knows it better than I.

I added 'solid line black' to this line:

 sb.Append("<table border='1px' solid line black cellpadding='5' cellspacing='0' ")

But no joy....

Community
  • 1
  • 1
user3486773
  • 1,174
  • 3
  • 25
  • 50

1 Answers1

2

Just add an inline style at the point at which you're generating the table cells.

sb.AppendFormat("<td style=\"border:solid 1px black\">{0}</td>", cellValue);
Grant Winney
  • 65,241
  • 13
  • 115
  • 165