Setting Rowspan for Dynamic Rows in asp.net Table Control
<p>I've created an asp.net table and added rows to the table <strong>dynamically</strong> that is using Backend ( C# ) . </p>

<p>The First Column of the Rows should have <strong>Rowspan</strong> .</p>

<p>The rows are being added very fine and the Rowspan is also added fine but when the <strong>Postback</strong> occurs , The <strong>Rowspans gets changes automatically</strong> .</p>

<p>Can any one please help me to solve this problem :(</p>

<p><strong>This is when the page loads First Time:</strong></p>

<p><a href="https://i.stack.imgur.com/nCtvZ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nCtvZ.jpg" alt="page first load"></a>
This is when <strong>PostBack Occured First Time :</strong></p>

<p><a href="https://i.stack.imgur.com/2oSBc.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2oSBc.jpg" alt="PostBack first time"></a>
This is when <strong>Postback Occured Second Time :</strong></p>

<p><a href="https://i.stack.imgur.com/s6nhm.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/s6nhm.jpg" alt="PostBack second time"></a>
Thanks in advance</p>

<p>Here is my complete code :</p>

<p><strong>.aspx Code</strong></p>

<pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head runat="server"&gt;
    &lt;title&gt;&lt;/title&gt;
    &lt;style&gt;
        .tbl {
        border:1px solid #000000;
        }
         .tbl td,th {
        border:1px solid #000000;
        }

    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;form id="form1" runat="server"&gt;
    &lt;div&gt;
     &lt;asp:Table runat="server" ID="tbl_fund" CssClass="tbl"&gt;&lt;/asp:Table&gt;
        &lt;asp:Button runat="server" Text="Do Post Back" /&gt;
    &lt;/div&gt;
    &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p><strong>C# Code :</strong></p>

<pre><code> protected void Page_Load(object sender, EventArgs e)
    {
        PrintFundChanges();
    }
    public void PrintFundChanges()
    {
        //***************************Header Started*****************************
        TableRow tr_head = new TableRow();
        TableHeaderCell th1 = new TableHeaderCell();

        th1.Text = "CNIC";
        TableHeaderCell th2 = new TableHeaderCell();
        th2.Text = "Field Name";
        TableHeaderCell th3 = new TableHeaderCell();
        th3.Text = "Updated Value";
        TableHeaderCell th4 = new TableHeaderCell();
        th4.Text = "Current Value";

        TableHeaderCell th5 = new TableHeaderCell();
        CheckBox chk_head = new CheckBox();
        chk_head.ID = "chk_fund";
        chk_head.CheckedChanged += new EventHandler(Chk_Fund_CheckedChanged);
        chk_head.Text = "Approve / Disapprove";
        chk_head.AutoPostBack = true;
        th5.Controls.Add(chk_head);

        TableHeaderCell th6 = new TableHeaderCell();
        th6.Text = "Table Name";
        th6.Style.Add("display", "none");
        TableHeaderCell th7 = new TableHeaderCell();
        th7.Text = "Column Name";
        th7.Style.Add("display", "none");
        TableHeaderCell th8 = new TableHeaderCell();
        th8.Text = "pk_code";
        th8.Style.Add("display", "none");
        TableHeaderCell th9 = new TableHeaderCell();
        th9.Text = "pk2_code";
        th9.Style.Add("display", "none");
        TableHeaderCell th10 = new TableHeaderCell();
        th10.Text = "action_type";
        th10.Style.Add("display", "none");

        th1.Style.Add("width", "20%");
        th2.Style.Add("width", "20%");
        th3.Style.Add("width", "20%");
        th4.Style.Add("width", "20%");

        tr_head.Cells.Add(th1);
        tr_head.Cells.Add(th2);
        tr_head.Cells.Add(th3);
        tr_head.Cells.Add(th4);
        tr_head.Cells.Add(th5);
        tr_head.Cells.Add(th6);
        tr_head.Cells.Add(th7);
        tr_head.Cells.Add(th8);
        tr_head.Cells.Add(th9);
        tr_head.Cells.Add(th10);
        tbl_fund.Rows.Add(tr_head);

        //****************************Header Ended*******************************
        //****************************Print Updated Data Started*************************

        //Getting distinct primary key data
        //DataTable dt_DistinctPk = dt_FundUpdatedFields.DefaultView.ToTable(true, "pk_code");
        DataTable dt_DistinctPk = new DataTable();
        dt_DistinctPk.Columns.Add(new DataColumn("pk_code", typeof(string)));
        DataRow dr = dt_DistinctPk.NewRow();
        dr["pk_code"] = "123";
        dt_DistinctPk.Rows.Add(dr);
        dr = dt_DistinctPk.NewRow();
        dr["pk_code"] = "456";
        dt_DistinctPk.Rows.Add(dr);

        for (int i = 0; i &lt; dt_DistinctPk.Rows.Count; i++)
        {
            TableCell td_pk = new TableCell();
            td_pk.Text = "&lt;b style='font-size:13px !important;'&gt;" + dt_DistinctPk.Rows[i]["pk_code"].ToString() + "&lt;/b&gt;";
            //int numberOfRecords = dt_FundUpdatedFields.Select("pk_code = " + dt_DistinctPk.Rows[i]["pk_code"].ToString()).Length;
            int numberOfRecords =5;
            td_pk.RowSpan = numberOfRecords;
            bool IsAddAtOnce = true;

             for (int j = 0; j &lt; 5; j++)
             {            
                    string field_name_lable = "Column name here";                    
                    TableRow tr = new TableRow();
                    TableCell td1 = new TableCell();
                    td1.Text = field_name_lable;
                    TableCell td2 = new TableCell();
                    TableCell td3 = new TableCell();
                    td2.Text = "New Value here";
                    td3.Text = "Old Value here";                    
                    TableCell td4 = new TableCell();
                    CheckBox chk = new CheckBox();
                    chk.ID = "chk_" +j;
                    //td4.Controls.Add(chk);
                    TableCell td5 = new TableCell();
                    td5.Text = "tbl_name";
                    td5.Style.Add("display","none");                 
                    TableCell td6 = new TableCell();
                    td6.Text = "field_name";
                    td6.Style.Add("display", "none");
                    TableCell td7 = new TableCell();
                    td7.Text = j.ToString();
                    td7.Style.Add("display", "none");
                    TableCell td8 = new TableCell();
                    td8.Text = j.ToString();
                    td8.Style.Add("display", "none");
                    TableCell td9 = new TableCell();
                    td9.Text = "1";
                    td9.Style.Add("display", "none");

                if (IsAddAtOnce)
                    {
                        tr.Cells.Add(td_pk);
                        IsAddAtOnce = false;
                    }
                    tr.Cells.Add(td1);
                    tr.Cells.Add(td2);
                    tr.Cells.Add(td3);
                    tr.Cells.Add(td4);
                    tr.Cells.Add(td5);
                    tr.Cells.Add(td6);
                    tr.Cells.Add(td7);
                    tr.Cells.Add(td8);
                    tr.Cells.Add(td9);
                    tbl_fund.Rows.Add(tr);
                if (j ==5 - 1)//Last Termination of Loop
                {
                    //Printing Attachments                 

                            td_pk.RowSpan = td_pk.RowSpan + 1;
                            td1 = new TableCell();
                            td1.Text = "CNIC Attachment";
                            td2 = new TableCell();
                            Image img_new = new Image();
                           // img_new.ImageUrl = "ImgHandler.ashx?typ=Newfund&amp;emp=" + Requestor + "&amp;pk=" + dt_FundUpdatedAttachments.Rows[k]["pk_code"].ToString();

                            td2.Controls.Add(img_new);
                            td3 = new TableCell();
                            Image img_old = new Image();
                            //img_old.ImageUrl = "ImgHandler.ashx?typ=Currfund&amp;emp=" + Requestor + "&amp;pk=" + dt_FundUpdatedAttachments.Rows[k]["pk_code"].ToString();

                            td3.Controls.Add(img_old);
                            td4 = new TableCell();
                            chk = new CheckBox();
                            //chk.ID = "chk_" + dt_FundUpdatedAttachments.Rows[k]["tbl_name"].ToString().ToUpper() + "_" + dt_FundUpdatedAttachments.Rows[k]["field_name"].ToString().ToUpper() + "_" + dt_FundUpdatedAttachments.Rows[k]["pk_code"].ToString().ToUpper() + "_";
                            td4.Controls.Add(chk);
                            td5 = new TableCell();
                            td5.Text = "tbl_name";
                            td5.Style.Add("display", "none");
                            td6 = new TableCell();
                            td6.Text ="field_name";
                            td6.Style.Add("display", "none");
                            td7 = new TableCell();
                            td7.Text = j.ToString();
                            td7.Style.Add("display", "none");
                            td8 = new TableCell();
                            td8.Text = "";
                            td8.Style.Add("display", "none");
                            td9 = new TableCell();
                            td9.Text = "1";
                            td9.Style.Add("display", "none");
                            tr = new TableRow();
                            tr.Cells.Add(td1);
                            tr.Cells.Add(td2);
                            tr.Cells.Add(td3);
                            tr.Cells.Add(td4);
                            tr.Cells.Add(td5);
                            tr.Cells.Add(td6);
                            tr.Cells.Add(td7);
                            tr.Cells.Add(td8);//pk2_code
                            tr.Cells.Add(td9);//action_type
                            tbl_fund.Rows.Add(tr);

                }
            }
        }

        ////****************************Print New Data Ended***************************

            TableRow tr_blank = new TableRow();
            TableCell td_blank = new TableCell();
            td_blank.Text = "Deleted Data";
            td_blank.Style.Add("font-weight", "bold !important");
            td_blank.Style.Add("font-size", " 16px !important");
            td_blank.Style.Add("background-color", "#e3e3e3 !important");
            td_blank.ColumnSpan = 10;
            tr_blank.Cells.Add(td_blank);
            tbl_fund.Rows.Add(tr_blank);


        ////****************************Print Deleted Data Started***************************

        //****************************Print Deleted Data Ended***************************

    }
    protected void Chk_Fund_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk = (CheckBox)sender;
        bool IsChecked = chk.Checked;
        for (int i = 1; i &lt; tbl_fund.Rows.Count; i++)
        {
            string pk_code = "";
            string tbl_name = "";
            string col_name = "";
            string pk2_code = "";
            int CellsCount = tbl_fund.Rows[i].Cells.Count;
            /*In this table rowspan is used for printing cnic number,
             so the row who has rowspan will have cnic column in first termination ,
             while in second termination the cell count will decrease because the
             row before the current row will having the rowspan.
             */
            if (CellsCount == 10)
            {
                tbl_name = tbl_fund.Rows[i].Cells[5].Text;
                col_name = tbl_fund.Rows[i].Cells[6].Text;
                pk_code = tbl_fund.Rows[i].Cells[7].Text;
                pk2_code = tbl_fund.Rows[i].Cells[8].Text;
            }
            else if (CellsCount == 9)
            {
                tbl_name = tbl_fund.Rows[i].Cells[4].Text;
                col_name = tbl_fund.Rows[i].Cells[5].Text;
                pk_code = tbl_fund.Rows[i].Cells[6].Text;
                pk2_code = tbl_fund.Rows[i].Cells[7].Text;
            }

            if (tbl_name != "")//if it is not a blank/header row
            {
                CheckBox ck = new CheckBox();
                string chk_id = "chk_" + tbl_name + "_" + col_name + "_" + pk_code + "_" + pk2_code;
                ck = (CheckBox)tbl_fund.Rows[i].Cells[4].FindControl(chk_id);//chk_tblname_colname_pk
                ck.Checked = IsChecked;
            }

        }
    }
</code></pre>