{
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;
}
The GridView control is able to convert DataGridView to HTML table in C# to viewing in a web page.
ReplyDelete