Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a textbox. which accept numeric field. depending on value in textbox that number of rows are appended to grid view. how to do this ?
Posted
Comments
Abdul Samad KP 11-Sep-14 8:27am    
How you are binding your grid?

1 solution

Sample Grid Code:

<asp:gridview id="Gridview1" runat="server" showfooter="true" autogeneratecolumns="false" xmlns:asp="#unknown">
         <columns>
         <asp:boundfield datafield="RowNumber" headertext="Row Number" />
         <asp:templatefield headertext="Header 1">
             <itemtemplate>
                 <asp:textbox id="TextBox1" runat="server"></asp:textbox>
             </itemtemplate>
         </asp:templatefield>
         <asp:templatefield headertext="Header 2">
             <itemtemplate>
                 <asp:textbox id="TextBox2" runat="server"></asp:textbox>
             </itemtemplate>
         </asp:templatefield>
         <asp:templatefield headertext="Header 3">
             <itemtemplate>
                  <asp:textbox id="TextBox3" runat="server"></asp:textbox>
             </itemtemplate>
             <footerstyle horizontalalign="Right" />
             <footertemplate>
              <asp:button id="ButtonAdd" runat="server" text="Add New Row">
                     onclick="ButtonAdd_Click" />
             </asp:button></footertemplate>
         </asp:templatefield>
         </columns>
     </asp:gridview>


Code to Set initial row

private void SetInitialRow()
  {
      DataTable dt = new DataTable();
      DataRow dr = null;
      dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
      dt.Columns.Add(new DataColumn("Column1", typeof(string)));
      dt.Columns.Add(new DataColumn("Column2", typeof(string)));
      dt.Columns.Add(new DataColumn("Column3", typeof(string)));
      dr = dt.NewRow();
      dr["RowNumber"] = 1;
      dr["Column1"] = string.Empty;
      dr["Column2"] = string.Empty;
      dr["Column3"] = string.Empty;
      dt.Rows.Add(dr);

      //Store the DataTable in ViewState
      ViewState["CurrentTable"] = dt;

      Gridview1.DataSource = dt;
      Gridview1.DataBind();
  }


To Add New Row

private void AddNewRowToGrid()
    {
        int j=0;
        while(j<convert.toint16(txtinputtextvalue.text))>
        {
        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    //extract the TextBox values
                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");

                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;

                    dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
                    dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
                    dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;

                    rowIndex++;
                }
                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["CurrentTable"] = dtCurrentTable;

                Gridview1.DataSource = dtCurrentTable;
                Gridview1.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }

        //Set Previous Data on Postbacks
        SetPreviousData();
            j++;
        }
        
    }



txtInputtextValue.text which is your text box for count of new rows

To set previous Data


private void SetPreviousData()
    {
        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");

                    box1.Text = dt.Rows[i]["Column1"].ToString();
                    box2.Text = dt.Rows[i]["Column2"].ToString();
                    box3.Text = dt.Rows[i]["Column3"].ToString();

                    rowIndex++;
                }
            }
        }
    }



Call the initial value method in page load event

call add new row method at button click

Note: if you bind data from DB Call that data as initial value
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900