Showing posts with label How to Convert DataGridView into HTML Table. Show all posts
Showing posts with label How to Convert DataGridView into HTML Table. Show all posts

Monday, May 11, 2009

How to Convert DataGridView into HTML Table

public static StringBuilder DataGridToHMTLTable(System.Windows.Forms.DataGridView dg)
{
StringBuilder sb = new StringBuilder();
//Setting HTML and table tags
sb.AppendLine("
<" +
"table border='2' cellpadding='1' cellspacing='1'>");
sb.AppendLine("");
//setting table headers
for (int i = 0; i < dg.Columns.Count; i++)
{
sb.AppendLine("" +
dg.Columns[i].HeaderText + "");
}
//Table body
sb.AppendLine("");
for (int i = 0; i < dg.Rows.Count; i++)
{
sb.AppendLine("");
foreach (DataGridViewCell dgvc in dg.Rows[i].Cells)
{
sb.AppendLine("" +
dgvc.Value.ToString() + "");
}
sb.AppendLine("");

}
//Table Footer and End of HTML Tag
sb.AppendLine("
");
return sb;
}
 
Locations of visitors to this page