If it were me, I'd create a class that represents your row data, and that has properties defined with appropriate types that match the column definitions in the
datatable
:
public class MyItem
{
public string OType { get; set; }
....
public DateTime DueDate { get; set; }
}
The constructor for that class would be something like this:
public MyItem(DataRow row)
{
this.OType = this.GetColumnValue("oType", "UNK");
...
this.DueDate = this.GetColumnValue("DueDate", new DateTime(0));
}
public T GetColumnValue<t>(DataRow row, string colName, T defaultValue)
{
object value = row[colName];
T result = (value != null) ? (T)value : defaultValue;
return result;
}
And add a property that returns the html table row already formatted.
public string AsHtmlTableRow
{
get
{
StringBuilder html = new StringBuilder();
html.AppendLine(" <tr>");
html.AppendFormat(" <td (with cell styles)>{0}</td>", this.OType).AppendLine();
...
html.AppendFormat(" <td (with cell styles)>{0}</td>", this.DueDate.ToString("dd/MM/yyyy")).AppendLine();
html.AppendLine(" </tr>");
return html.ToString();
}
}
At that point, all you have to do is this
List<MyItem> items = new List<MyItem>();
foreach(DataRow row in dataTable.Rows)
{
items.Add(new MyItem(row));
}
StringBuilder table = new StringBuilder();
table.AppendLine("<table (with table styles)>");
foreach(MyItem item in items)
{
table.Append(item.AsHtmlDataRow);
}
table.AppendLine("</table>");
Keep in mind that the code above is just generalized pseudo code, so you you'll need to tweak it here and there, and mold it to your specific needs. I don't feel the need to do that myself since it's your code, and I feel a strong need right now to take a righteous dump.
BTW, a minor nit - create some CSS style classes so that your html isn't fulll of repetitive CSS elements. That just trashes up the html and makes it hard to read.