Add Library --
using System.Web.UI.HtmlControls;
public static void ConvertDataTableToHTMLWithHTmlGenericContorl(DataTable dt)
{
HtmlGenericControl table = new HtmlGenericControl("table"); //Table create
table.Attributes.Add("class", "classname"); //assign class
HtmlGenericControl thead = new HtmlGenericControl("thead"); // add thead tag
HtmlGenericControl tr = new HtmlGenericControl("tr"); //add header row
for (int i = 0; i < dt.Columns.Count; i++)
{
HtmlGenericControl th = new HtmlGenericControl("th"); // add column of header
th.InnerText = dt.Columns[i].ColumnName.ToString();
th.Attributes.Add("class", "classname");
tr.Controls.Add(th); // add every th in header row
}
thead.Controls.Add(tr); // header row add in thead
table.Controls.Add(thead); //thead add in table
HtmlGenericControl tbody = new HtmlGenericControl("tbody"); //add tbody tag
//for datatable row count loop
for (int i = 0; i < dt.Rows.Count; i++)
{
HtmlGenericControl dataTR = new HtmlGenericControl("tr"); // add row
for (int j = 0; j < dt.Columns.Count; j++)
{
HtmlGenericControl td = new HtmlGenericControl("td"); // add column
td.InnerText = dt.Rows[i][j].ToString();
td.Attributes.Add("class", "classname");
dataTR.Controls.Add(td);//add every column in row
}
tbody.Controls.Add(dataTR); //row will add in tbody tag
}
table.Controls.Add(tbody); // tbody all row will add in table.
//Finaly table content add in your page div id
DataDiv.Controls.Add(table);
}
using System.Web.UI.HtmlControls;
public static void ConvertDataTableToHTMLWithHTmlGenericContorl(DataTable dt)
{
HtmlGenericControl table = new HtmlGenericControl("table"); //Table create
table.Attributes.Add("class", "classname"); //assign class
HtmlGenericControl thead = new HtmlGenericControl("thead"); // add thead tag
HtmlGenericControl tr = new HtmlGenericControl("tr"); //add header row
for (int i = 0; i < dt.Columns.Count; i++)
{
HtmlGenericControl th = new HtmlGenericControl("th"); // add column of header
th.InnerText = dt.Columns[i].ColumnName.ToString();
th.Attributes.Add("class", "classname");
tr.Controls.Add(th); // add every th in header row
}
thead.Controls.Add(tr); // header row add in thead
table.Controls.Add(thead); //thead add in table
HtmlGenericControl tbody = new HtmlGenericControl("tbody"); //add tbody tag
//for datatable row count loop
for (int i = 0; i < dt.Rows.Count; i++)
{
HtmlGenericControl dataTR = new HtmlGenericControl("tr"); // add row
for (int j = 0; j < dt.Columns.Count; j++)
{
HtmlGenericControl td = new HtmlGenericControl("td"); // add column
td.InnerText = dt.Rows[i][j].ToString();
td.Attributes.Add("class", "classname");
dataTR.Controls.Add(td);//add every column in row
}
tbody.Controls.Add(dataTR); //row will add in tbody tag
}
table.Controls.Add(tbody); // tbody all row will add in table.
//Finaly table content add in your page div id
DataDiv.Controls.Add(table);
}
No comments:
Post a Comment