Dynamic table creation in asp.net using c# :
Aspx
<asp:Panel ID="pnl" runat="server">
</asp:Panel>
Aspx.cs
string
Select;
SqlConnection Conn =
new
SqlConnection(ConfigurationManager.AppSettings["Connection"]);
SqlDataAdapter da =
new SqlDataAdapter();
DataSet ds = new
DataSet();
int
Count;
protected void
Page_Load(object sender,
EventArgs e)
{
tblCreation();
}
//Method Call
public void
tblCreation()
{
Select =
"Select * From Attributelist";
da =
new SqlDataAdapter(Select, Conn);
DataSet dsTest = new
DataSet();
da.Fill(dsTest);
int CountTest;
CountTest =
dsTest.Tables[0].Rows.Count;
--Table
Creation
Table tbldynamicTest
= new Table();
--Table Row <tr>
TableRow trTest =
new TableRow();
--Table Data<td>
TableCell tcTest
= new TableCell();
--Label
Label lblHead
= new Label();
trTest.Height =
20;
Label lblID1 = new
Label();
tcTest.Width =
100;
lblID1.Height =
20;
lblID1.ID =
"lb1";
lblID1.Text =
"Name";
lblID1.Width =
100;
--TC Add Label
tcTest.Controls.Add(lblID1);
--TR Add tc
trTest.Cells.Add(tcTest);
--TableAdd tr
tbldynamicTest.Rows.Add(trTest);
for (int i = 0;
i < CountTest; i++)
{
trTest.Height
= 20;
Label lblName
= new Label();
lblName.ID =
"lblName" + i;
lblName.Text
= dsTest.Tables[0].Rows[i]["name"].ToString();
lblName.CssClass = "lbl";
lblName.Width
= 100;
--TC Add Label
tcTest.Controls.Add(lblName);
--TR Add tc
trTest.Cells.Add(tcTest);
}
--Table Rows Add
tbldynamicTest.Rows.Add(trTest);
--Panel
Add
pnl.Controls.Add(tbldynamicTest);
for (int i = 0;
i < CountTest; i++)
{
TableRow tr
= new TableRow();
TableCell tc
= new TableCell();
tr.Height =
20;
Label lblName = new
Label();
lblName.ID =
"lblName" + i;
lblName.Text
= dsTest.Tables[0].Rows[i]["name"].ToString();
lblName.CssClass = "lbl";
lblName.Width
= 100;
tc.Controls.Add(lblName);
tr.Cells.Add(tc);
for (int j = 0;
j < Count; j++)
{
--Checkbox Create
CheckBox chk =
new CheckBox();
chk.ID =
"chk" + j;
chk.Height =
25;
chk.CssClass = "txt";
chk.Width
= 100;
--TC Add Checkbox
tc.Controls.Add(chk);
--TR Add TC
tr.Cells.Add(tc);
}
--Table Add TR
tbldynamicTest.Rows.Add(tr);
--Panel
add Table
pnl.Controls.Add(tbldynamicTest);
}
--Panel
add Table
pnl.Controls.Add(tbldynamicTest);
}