I think its better to create the entire HTML
table from the code behind.
For this we can add a literal and use it in the code behind to add a table structure from the code behind.
e.g. Lets make an HTML
table of Birds detail.
Just to keep explanation short I have not included the data table part filled with birds detail.
if (dtBirdsDetail.Rows.Count > 0)
{
litBirdsTable.Text = "<center><table id='tbldata' cellspacing='0' cellpadding='1' border='1' style='border-collapse: collapse;'>" + System.Environment.NewLine;
litBirdsTable.Text += "<tr>";
//add datatable columns to html table as heading
for (int liColumnIndex = 0; liColumnIndex < dtBirdsDetail.Columns.Count;liColumnIndex++)
{
litBirdsTable.Text += "<th>" + dtBirdsDetail.Columns[liColumnIndex].ColumnName
+ "</th>" + System.Environment.NewLine;
}
litBirdsTable.Text += System.Environment.NewLine + "</tr>";
//add datatable rows to html table
for (int liRowIndex = 0; liRowIndex < dtBirdsDetail.Rows.Count; liRowIndex++)
{
litBirdsTable.Text += "<tr>";
litBirdsTable.Text += "<td>" + dtBirdsDetail.Rows[liRowIndex]["ID"] + "</td>" +
System.Environment.NewLine;
litBirdsTable.Text += "<td>" + dtBirdsDetail.Rows[liRowIndex]["BirdName"] + "
</td>" + System.Environment.NewLine;
litBirdsTable.Text += "<td>" + dtBirdsDetail.Rows[liRowIndex]["TypeOfBird"] + "
</td>" + System.Environment.NewLine;
litBirdsTable.Text += "<td>" + dtBirdsDetail.Rows[liRowIndex]["ScientificName"]
+ "</td>" + System.Environment.NewLine;
litBirdsTable.Text += "</tr>";
}
litBirdsTable.Text += "</table></center>";
}
For detailed explanation, visit this link:
http://codevariation.blogspot.com/2018/03/html-table-design-with-dynamic-data.html