- Asp.net Code
<div>
<asp:GridView ID="gv" runat="server"></asp:GridView>
<br />
COL:
<asp:Label ID="lblCol" runat="server"></asp:Label><br />
ROW:
<asp:Label ID="lblRow" runat="server"></asp:Label><br />
<asp:GridView ID="gv1" runat="server"></asp:GridView>
<br />
<div id="divTitle" runat="server"></div>
</div>
- C# Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
namespace DynamicTable
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable table = new DataTable();
table.Columns.Add("Job", typeof(string));
table.Columns.Add("Col", typeof(int));
table.Columns.Add("Row", typeof(int));
table.Rows.Add("Data 1", 1, 1);
table.Rows.Add("Data 2", 2, 1);
table.Rows.Add("Data 3", 3, 1);
table.Rows.Add("Data 4", 4, 1);
table.Rows.Add("Data 5", 5, 1);
table.Rows.Add("Data 6", 6, 1);
table.Rows.Add("Data 7", 7, 1);
table.Rows.Add("Data 8", 8, 1);
table.Rows.Add("Data 9", 1, 2);
table.Rows.Add("Data 10", 2, 2);
table.Rows.Add("Data 11", 3, 2);
table.Rows.Add("Data 12", 4, 2);
table.Rows.Add("Data 13", 5, 2);
table.Rows.Add("Data 14", 6, 2);
table.Rows.Add("Data 15", 7, 2);
table.Rows.Add("Data 16", 8, 2);
table.Rows.Add("Data 17", 1, 3);
table.Rows.Add("Data 18", 1, 4);
table.Rows.Add("Data 19", 8, 3);
table.Rows.Add("Data 20", 8, 4);
table.Rows.Add("Data 21", 8, 5);
table.Rows.Add("Data 22", 8, 6);
table.Rows.Add("Data 23", 8, 7);
table.Rows.Add("Data 24", 8, 8);
table.Rows.Add("Data 25", 8, 9);
gv.DataSource = table;
gv.DataBind();
int col = Convert.ToInt32(table.AsEnumerable().Max(row1 => row1["Col"]));
int row = Convert.ToInt32(table.AsEnumerable().Max(row1 => row1["Row"]));
lblCol.Text = col.ToString();
lblRow.Text = row.ToString();
DataTable dt = new DataTable();
for (int i = 1; i <= col; i++)
{
dt.Columns.Add(i.ToString(), typeof(string));
}
for (int i = 1; i <= row; i++)
{
DataRow workRow;
workRow = dt.NewRow();
for (int j = 0; j < col; j++)
{
DataRow drow = table.AsEnumerable().Where(p => p.Field<Int32>("Col") == j + 1 && p.Field<Int32>("Row") == i).FirstOrDefault();
if (drow == null)
{
// workRow[j] = "(" + i.ToString() + "," + (j + 1).ToString() + ")";
workRow[j] = "No_Data";
}
else
{
workRow[j] = drow["Job"].ToString();
}
}
dt.Rows.Add(workRow);
}
gv1.DataSource = dt;
gv1.DataBind();
divTitle.InnerHtml = ExportDatatableToHtml(dt);
}
protected string ExportDatatableToHtml(DataTable dt)
{
StringBuilder strHTMLBuilder = new StringBuilder();
strHTMLBuilder.Append("<table>");
strHTMLBuilder.Append("<tr >");
foreach (DataColumn myColumn in dt.Columns)
{
strHTMLBuilder.Append("<td><div style='background-color:red;'>");
strHTMLBuilder.Append(myColumn.ColumnName);
strHTMLBuilder.Append("</div></td>");
}
strHTMLBuilder.Append("</tr>");
foreach (DataRow myRow in dt.Rows)
{
strHTMLBuilder.Append("<tr >");
foreach (DataColumn myColumn in dt.Columns)
{
strHTMLBuilder.Append("<td >");
if (myRow[myColumn.ColumnName].ToString() != "No_Data")
{
strHTMLBuilder.Append("<div style='background-color:green;'>");
strHTMLBuilder.Append(myRow[myColumn.ColumnName].ToString());
strHTMLBuilder.Append("</div>");
}
strHTMLBuilder.Append("</td>");
}
strHTMLBuilder.Append("</tr>");
}
//Close tags.
strHTMLBuilder.Append("</table>");
string Htmltext = strHTMLBuilder.ToString();
return Htmltext;
}
}
}
No comments:
Post a Comment