create dynamic table in asp.net c#

Hello Everyone,

I'm going to share the code sample for create dynamic table using asp.net c#.

Today, i have a requirement to create a dynamic table with the help of user inputs( number of rows and columns) on click on create table button.

These look like the below:


















In the 1st step

I'm going to share the code sample for .aspx page


<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <div>
        No. of Rows: <asp:TextBox ID="txt_TableRows" runat="server" Width="65px" />&nbsp;
        No. of Cols: <asp:TextBox ID="txt_TableCols" runat="server" Width="65px" /> <br />

        <asp:CheckBox ID="chk_CellBorder" runat="server" Text="Broder of cells" />&nbsp;
        <asp:Button ID="btn_createTable" CssClass="btnColor" OnClick="btn_createTable_Click" runat="server" Text="Create Table" />
   
        <asp:Table ID="tblTable" runat="server" />
  </div>
</asp:Content>



In the 2nd step

I'm going to share the code sample for .cs page


namespace Create_dynamic_table
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Add to border style.
            tblTable.BorderStyle = BorderStyle.Inset;
            tblTable.BorderWidth = Unit.Pixel(2);
        }

        /// <summary>
        /// button event for create table
        /// </summary>
        protected void btn_createTable_Click(object sender, System.EventArgs e)
        {          
            //Input row and column.
            int RowNo = Int32.Parse(txt_TableRows.Text);
            int ColsNo = Int32.Parse(txt_TableCols.Text);

            //Clear table.
            tblTable.Controls.Clear();

            if (RowNo > 0 && ColsNo > 0)
            {
                //Used for creat row.
                for (int i = 0; i < RowNo; i++)
                {
                    TableRow Rows = new TableRow();
                    tblTable.Controls.Add(Rows);

                    //Used for creat cells.
                    for (int j = 0; j < ColsNo; j++)
                    {
                        TableCell cells = new TableCell();
                        Label lbl = new Label();
                        lbl.Text = "[" + i.ToString() + "," + j.ToString() + "]<br />";
                        cells.Controls.Add(lbl);

                        if (chk_CellBorder.Checked == true)
                        {
                            cells.BorderStyle = BorderStyle.Inset;
                            cells.BorderWidth = Unit.Pixel(2);
                        }
                        Rows.Controls.Add(cells);

                    }
                }
            }
            else {
                Response.Write("Please enter no. of rows and cols");
            }
        }
    }
}






ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.
^