我正在使用C#的HtmlTextWriter创建一个HTML表格 当我循环创建我的表头时,它会以这种格式创建它
<tr><th>colname</th><th>colname</th><th>colname</th><th>colname</th></tr>
我希望<tr>
说<thead>
而<th>
说<td>
。
如何编程HtmlTextWriter来更改标记值?
这是我创建标题的循环。
public void BuildHTMLTable(DataTable dt)
{
StringWriter sw = new StringWriter();
HtmlTextWriter w = new HtmlTextWriter(sw);
Table tbl = new Table();
tbl.ID = "example";
tbl.CssClass = "display";
tbl.CellSpacing = 0;
TableHeaderRow thr = new TableHeaderRow();
foreach (DataColumn col in dt.Columns)
{
TableCell th = new TableCell();
th.Text = col.Caption;
thr.Controls.Add(th);
}
tbl.Controls.Add(thr);
foreach (DataRow row in dt.Rows)
{
TableRow tr = new TableRow();
foreach (var value in row.ItemArray)
{
TableCell td = new TableCell();
td.Text = value.ToString();
tr.Controls.Add(td);
}
tbl.Controls.Add(tr);
}
TableFooterRow tfr = new TableFooterRow();
foreach (DataColumn col in dt.Columns)
{
TableCell th = new TableCell();
th.Text = col.Caption;
tfr.Controls.Add(th);
}
tbl.Controls.Add(tfr);
tbl.RenderControl(w);
tableString = sw.ToString();
}