convert datatable into html table

Rajni.Shekhar
Posted by Rajni.Shekhar under ASP.NET category on | Points: 40 | Views : 4707
public string CreateHtmlTableRows(DataTable targetTable, string strtblName)
{
StringBuilder myBuilder = new StringBuilder();
if (strtblName != "")
myBuilder.Append("<table Id='" + strtblName + "' border='0px' cellpadding='0' cellspacing='0' ");
else
myBuilder.Append("<table border='0px' cellpadding='0' cellspacing='0' ");
myBuilder.Append("style='width: 100%;'>");
//Add the headings row.
myBuilder.Append("<tr align='left' valign='top' class='grdHeader'>"); //grdHeader class will be in CSS file

foreach (DataColumn myColumn in targetTable.Columns)
{
myBuilder.Append("<td align='left' valign='top'>");
myBuilder.Append(myColumn.ColumnName);
myBuilder.Append("</td>");
}
myBuilder.Append("</tr>");
//Add the data rows.
int intI = 0;
int totRow = targetTable.Rows.Count-1;
foreach (DataRow myRow in targetTable.Rows)
{

if (intI % 2 == 0)
myBuilder.Append("<tr align='left' class='grdOddRow'>"); //'grdOddRow'class will be in CSS file
else
myBuilder.Append("<tr align='left' class='grdEvenRow'>"); //''grdEvenRow''class will be in CSS file

foreach (DataColumn myColumn in targetTable.Columns)
{
myBuilder.Append("<td align='left' valign='top'><b>");
myBuilder.Append(myRow[myColumn.ColumnName].ToString());
myBuilder.Append("</b></td>");
}
myBuilder.Append("</tr>");
intI += 1;
}
//Close tags.
return myBuilder.Append("</table>").ToString();

Comments or Responses

Login to post response