Create Table Dynamically from Data table When Column of Data Table conver to Table Cell or row.
Supoose We Have Table Employee
DataTable dt1;
DataTable dt2;
Table tb = new Table();
tb.CssClass = "mGrid";
Int32 k = 1;
string lastvalue = "";
for (int i = 0; i < dt.Rows.Count; i++)
{
String empNo = dt1.Rows[i][0].ToString();
TableRow tbrow = new TableRow();
TableRow tbr = new TableRow();
TableRow tbr1 = new TableRow();
if (i == 0)
{
TableHeaderCell tbHcell = new TableHeaderCell();//Header Row for Serial No
tbHcell.Width = 60;
tbHcell.BackColor = System.Drawing.Color.Black;
tbHcell.ForeColor = System.Drawing.Color.White;
tbHcell.BorderWidth = 1;
tbHcell.RowSpan = 2;
tbHcell.Text = "SNo";
tbr1.Cells.Add(tbHcell);
TableHeaderCell tbHcell1 = new TableHeaderCell();//Header Row for emp No
tbHcell1.Width = 100;
tbHcell1.BackColor = System.Drawing.Color.Black;
tbHcell1.ForeColor = System.Drawing.Color.White;
tbHcell1.BorderWidth = 1;
tbHcell1.RowSpan = 2;
tbHcell1.Text = "empno";
tbr1.Cells.Add(tbHcell1);
}
TableCell tbcell = new TableCell();// Row value for serial No
tbcell.Width = 60;
tbcell.BorderWidth = 1;
tbcell.Text = (i + 1).ToString();
tbrow.Cells.Add(tbcell);
TableCell tbcell1 = new TableCell();//Row value for emp No
tbcell1.Width = 100;
tbcell1.BorderWidth = 1;
tbcell1.Text = empno;
tbrow.Cells.Add(tbcell1);
TableHeaderCell tbHcell5 = new TableHeaderCell();//Tabel main Header from dt table rows
for (int j = 0; j < dt2.Rows.Count; j++)
{
if (i == 0)
{
TableHeaderCell tbHcell2 = new TableHeaderCell();//Tabel Header from dt table rows
tbHcell2.Width = 100;
tbHcell2.BackColor = System.Drawing.Color.Black;
tbHcell2.ForeColor = System.Drawing.Color.White;
tbHcell2.BorderWidth = 1;
tbHcell2.Text = dt2.Rows[j][1].ToString();
tbr.Cells.Add(tbHcell2);
if (lastvalue == dt.Rows[j][2].ToString())
{
k = k + 1;
}
else
{
tbHcell5 = new TableHeaderCell();//Tabel main Header with column Span from dt table rows
tbHcell5.Width = 100;
tbHcell5.BackColor = System.Drawing.Color.Black;
tbHcell5.ForeColor = System.Drawing.Color.White;
tbHcell5.BorderWidth = 1;
tbHcell5.Text = dt2.Rows[j][2].ToString();
tbr1.Cells.Add(tbHcell5);
lastvalue = dt2.Rows[j][2].ToString();
k = 1;
}
tbHcell5.ColumnSpan = k;
}
TableCell tbcell2 = new TableCell();//table row value from dt table row
tbcell2.Width = 100;
tbcell2.BorderWidth = 1;
tbcell2.Text = Convert.ToDouble(dt2.Rows[j][7]).ToString("N2");
tbrow.Cells.Add(tbcell2);
}
if (i == 0)
{
tb.Rows.Add(tbr1);
tb.Rows.Add(tbr);
}
tb.Rows.Add(tbrow);
}
Panel1.Controls.Add(tb);
}
Supoose We Have Table Employee
DataTable dt1;
DataTable dt2;
Table tb = new Table();
tb.CssClass = "mGrid";
Int32 k = 1;
string lastvalue = "";
for (int i = 0; i < dt.Rows.Count; i++)
{
String empNo = dt1.Rows[i][0].ToString();
TableRow tbrow = new TableRow();
TableRow tbr = new TableRow();
TableRow tbr1 = new TableRow();
if (i == 0)
{
TableHeaderCell tbHcell = new TableHeaderCell();//Header Row for Serial No
tbHcell.Width = 60;
tbHcell.BackColor = System.Drawing.Color.Black;
tbHcell.ForeColor = System.Drawing.Color.White;
tbHcell.BorderWidth = 1;
tbHcell.RowSpan = 2;
tbHcell.Text = "SNo";
tbr1.Cells.Add(tbHcell);
TableHeaderCell tbHcell1 = new TableHeaderCell();//Header Row for emp No
tbHcell1.Width = 100;
tbHcell1.BackColor = System.Drawing.Color.Black;
tbHcell1.ForeColor = System.Drawing.Color.White;
tbHcell1.BorderWidth = 1;
tbHcell1.RowSpan = 2;
tbHcell1.Text = "empno";
tbr1.Cells.Add(tbHcell1);
}
TableCell tbcell = new TableCell();// Row value for serial No
tbcell.Width = 60;
tbcell.BorderWidth = 1;
tbcell.Text = (i + 1).ToString();
tbrow.Cells.Add(tbcell);
TableCell tbcell1 = new TableCell();//Row value for emp No
tbcell1.Width = 100;
tbcell1.BorderWidth = 1;
tbcell1.Text = empno;
tbrow.Cells.Add(tbcell1);
TableHeaderCell tbHcell5 = new TableHeaderCell();//Tabel main Header from dt table rows
for (int j = 0; j < dt2.Rows.Count; j++)
{
if (i == 0)
{
TableHeaderCell tbHcell2 = new TableHeaderCell();//Tabel Header from dt table rows
tbHcell2.Width = 100;
tbHcell2.BackColor = System.Drawing.Color.Black;
tbHcell2.ForeColor = System.Drawing.Color.White;
tbHcell2.BorderWidth = 1;
tbHcell2.Text = dt2.Rows[j][1].ToString();
tbr.Cells.Add(tbHcell2);
if (lastvalue == dt.Rows[j][2].ToString())
{
k = k + 1;
}
else
{
tbHcell5 = new TableHeaderCell();//Tabel main Header with column Span from dt table rows
tbHcell5.Width = 100;
tbHcell5.BackColor = System.Drawing.Color.Black;
tbHcell5.ForeColor = System.Drawing.Color.White;
tbHcell5.BorderWidth = 1;
tbHcell5.Text = dt2.Rows[j][2].ToString();
tbr1.Cells.Add(tbHcell5);
lastvalue = dt2.Rows[j][2].ToString();
k = 1;
}
tbHcell5.ColumnSpan = k;
}
TableCell tbcell2 = new TableCell();//table row value from dt table row
tbcell2.Width = 100;
tbcell2.BorderWidth = 1;
tbcell2.Text = Convert.ToDouble(dt2.Rows[j][7]).ToString("N2");
tbrow.Cells.Add(tbcell2);
}
if (i == 0)
{
tb.Rows.Add(tbr1);
tb.Rows.Add(tbr);
}
tb.Rows.Add(tbrow);
}
Panel1.Controls.Add(tb);
}
No comments:
Post a Comment