Dynamic table in the sense to create a data table dynamically and add rows one by one programmatically.
Create ASP.NET application and add a container control here PlaceHolder
Design Page will be like:
Create ASP.NET application and add a container control here PlaceHolder
Design Page will be like:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Dynamic
Table in Asp.Net using C#</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</div>
</form>
</body>
</html>
C# Code:
protected void Page_Load(object
sender, EventArgs e)
{
CreateDynamicTable();
}
private void CreateDynamicTable()
{
PlaceHolder1.Controls.Clear();
//initialize
number of rows and column for the table
int row =
5;
int col =
7;
//Create a Table
and set its properties
Table
tbl_dynamic = new Table();
// Add the table
to the placeholder control
PlaceHolder1.Controls.Add(tbl_dynamic);
//Add table
Header as per your requirement
for (int i = 0; i < 1; i++) //
header will be one
{
TableHeaderRow
thr = new TableHeaderRow();
for (int j = 0; j < col; j++)
{
TableHeaderCell
thc = new TableHeaderCell();
thc.BorderWidth = Unit.Pixel(1);
thc.BorderStyle = BorderStyle.Solid;
thc.Text = "HeaderRow:"
+ i + " " + "HeaderColumn:" + " " + j;
// Add the
TableHeaderCell to the TableRow
thr.Cells.Add(thc);
}
// Add the
TableHeaderRow to the Table
tbl_dynamic.Rows.Add(thr);
}
// Now iterate
through the table and add your controls
for (int i = 0; i < row; i++)
{
TableRow
tr = new TableRow();
for (int j = 0; j < col; j++)
{
TableCell
tc = new TableCell();
tc.BorderWidth = Unit.Pixel(1);
tc.BorderStyle = BorderStyle.Solid;
tc.Text = "RowNo:"
+ i + " " + "ColumnNo:" + "
" + j;
// Add
the TableCell to the TableRow
tr.Cells.Add(tc);
}
// Add the
TableRow to the Table
tbl_dynamic.Rows.Add(tr);
}
}
Reference: http://www.dotnetcurry.com/ShowArticle.aspx?ID=135
No comments:
Post a Comment