Dynamic Table Creation in ASP.Net


.aspx
<form id="form1" runat="server">
    <div>
        <asp:PlaceHolder ID="myplaceholder" runat="server"></asp:PlaceHolder>
    </div>
    </form>

.aspx.cs
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GenerateTable(GetData());
            }
        }
        private DataTable GetData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("S.No");
            dt.Columns.Add("Name");
            dt.Columns.Add("Address");

            for (int PK = 1; PK <= 10; PK++)
            {
                DataRow dr = dt.NewRow();
                dr["S.No"] = PK;
                dr["Name"] = "Name" + PK;
                dr["Address"] = "Address" + PK;
                dt.Rows.Add(dr);
                dt.AcceptChanges();
            }

            return dt;
        }
        private void GenerateTable(DataTable dt)
        {
            //Creat the Table and Add it to the Page
            Table table = new Table();
            table.ID = "Table1";
            table.BorderStyle = BorderStyle.Inset;
            table.BorderWidth = Unit.Pixel(4);
            table.CellPadding = 2;
            table.CellSpacing = 2;
            //Page.Form.Controls.Add(table);
            myplaceholder.Controls.Add(table);

            //The number of Columns to be generated
            int colsCount = dt.Columns.Count;

            TableRow header_row = new TableRow();

            for (int j = 0; j < colsCount; j++)
            {

                TableCell cell_header = new TableCell();
                cell_header.BorderStyle = BorderStyle.Double;
                cell_header.BorderWidth = Unit.Pixel(3);
                Label lbl_header = new Label();

                // Set a unique ID for each Label added
                lbl_header.ID = "Label_header_Col_" + j;
                // Set value for each Label added
                lbl_header.Text = dt.Columns[j].ToString();
                // Add the control to the TableCell
                cell_header.Controls.Add(lbl_header);
                // Add the TableCell to the TableRow
                header_row.Cells.Add(cell_header);

            }

            // And finally, add the TableRow to the Table
            table.Rows.Add(header_row);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                TableRow row = new TableRow();
                for (int j = 0; j < colsCount; j++)
                {
                    TableCell cell = new TableCell();
                    cell.BorderStyle = BorderStyle.Double;
                    cell.BorderWidth = Unit.Pixel(1);
                    Label lbl = new Label();

                    // Set a unique ID for each Label added
                    lbl.ID = "Label_Row_" + i + "Col_" + j;
                    // Set value for each Label added
                    lbl.Text = dt.Rows[i][j].ToString();
                    // Add the control to the TableCell
                    cell.Controls.Add(lbl);
                    // Add the TableCell to the TableRow
                    row.Cells.Add(cell);
                }

                // And finally, add the TableRow to the Table
                table.Rows.Add(row);
            }

        }

You Can Download the Working Code of Dynamic Table Creation in ASP.Net From here.