Dynamic Number Of Rows Of GridView With Controls

Sometimes, we need to have a number of rows with controls generated dynamically in the WebForm. Here, I am going to describe how to do it in a simple way.
 
Create an empty website. Let's take a WebForm, a textbox, and a button. In the text box, we will fill the desired number of rows of Gridview. And on the click of the button, the Gridview with controls will be shown. Let's take a simple Gridview. In the Gridview ItemTemplate, let's add a few textboxes. Let's take another button at the bottom of the click event which will access the values of controls in Gridview. The code is here.
  1. <asp:Panel ID="PnlOthr" runat="server"> Enter number of other contractors  
  2.     <asp:TextBox ID="TxtNo" runat="server" MaxLength="3" />  
  3.     <asp:Button ID="BtnAdd" runat="server" Text="Add" OnClick="BtnAdd_Click" />  
  4.     <asp:GridView runat="server" ID="gvOthrCntrctr" ShowFooter="true" AutoGenerateColumns="false">  
  5.         <Columns>  
  6.             <asp:TemplateField ItemStyle-HorizontalAlign="Center">  
  7.                 <HeaderTemplate> Sl No. </HeaderTemplate>  
  8.                 <ItemTemplate>  
  9.                     <asp:Label ID="lblSRNO" runat="server" Text='<%#Container.DataItemIndex+1 %>'></asp:Label>  
  10.                 </ItemTemplate>  
  11.             </asp:TemplateField>  
  12.             <asp:TemplateField HeaderText="Other Contractor Name">  
  13.                 <ItemTemplate>  
  14.                     <asp:TextBox ID="TxtOthrCntrctrNm" runat="server" />  
  15.                     <asp:RequiredFieldValidator ID="RequiredFieldValidator31" runat="server" ErrorMessage="*" ControlToValidate="TxtOthrCntrctrNm" ForeColor="Red" Display="Dynamic" ValidationGroup="y"></asp:RequiredFieldValidator>  
  16.                 </ItemTemplate>  
  17.             </asp:TemplateField>  
  18.             <asp:TemplateField HeaderText="Aadhar NO">  
  19.                 <ItemTemplate>  
  20.                     <asp:TextBox ID="TxtAadharNo" runat="server" MaxLength="12" />  
  21.                 </ItemTemplate>  
  22.             </asp:TemplateField>  
  23.             <asp:TemplateField HeaderText="PAN NO">  
  24.                 <ItemTemplate>  
  25.                     <asp:TextBox ID="TxtPANNo" runat="server" MaxLength="10" />  
  26.                     <asp:RequiredFieldValidator ID="RequiredFieldValidator336" runat="server" ErrorMessage="*" ControlToValidate="TxtPANNo" ForeColor="Red" Display="Dynamic" ValidationGroup="y"></asp:RequiredFieldValidator>  
  27.                 </ItemTemplate>  
  28.             </asp:TemplateField>  
  29.             <asp:TemplateField HeaderText="Other Contractor MobileNo">  
  30.                 <ItemTemplate>  
  31.                     <asp:TextBox ID="TxtOthrCntrctrMobileNo" MaxLength="10" runat="server" />  
  32.                     <asp:RequiredFieldValidator ID="RequiredFieldValidator32" runat="server" ErrorMessage="*" ControlToValidate="TxtOthrCntrctrMobileNo" ForeColor="Red" Display="Dynamic" ValidationGroup="y"></asp:RequiredFieldValidator>  
  33.                 </ItemTemplate>  
  34.             </asp:TemplateField>  
  35.             <asp:TemplateField HeaderText="Other Contractor Emailid">  
  36.                 <ItemTemplate>  
  37.                     <asp:TextBox ID="TxtOthrCntrctrEmailid" runat="server" />  
  38.                     <asp:RequiredFieldValidator ID="RequiredFieldValidator34" runat="server" ErrorMessage="*" ControlToValidate="TxtOthrCntrctrEmailid" ForeColor="Red" Display="Dynamic" ValidationGroup="y"></asp:RequiredFieldValidator>  
  39.                     <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Emailid In Correct Format." ValidationGroup="y" ForeColor="Red" ControlToValidate="TxtOthrCntrctrEmailid" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>  
  40.                 </ItemTemplate>  
  41.             </asp:TemplateField>  
  42.         </Columns>  
  43.     </asp:GridView>  
  44. </asp:Panel>  
  45. <div style="text-align:center;">  
  46.     <asp:Button ID="BtnShow" runat="server" Text="Show Gridview Data in Form" OnClick="BtnShow_Click" />  
  47. </div>   
Now, in the code behind, add the following code.
  1. protected void BtnAdd_Click(object sender, EventArgs e) {  
  2.     BindOthrGridview(Convert.ToInt16(TxtNo.Text.Trim()));  
  3. }  
  4. protected void BindOthrGridview(int rowsno) {  
  5.     DataTable dt = new DataTable();  
  6.     dt.Columns.AddRange(new DataColumn[5] {  
  7.         new DataColumn("ContractorNm"), new DataColumn("ContractorAdhaar"), new DataColumn("ContractorPAN"), new DataColumn("ConractorMObileNo"), new DataColumn("ContractorEmail")  
  8.     });  
  9.     for (int i = 0; i < rowsno; i++) {  
  10.         dt.Rows.Add();  
  11.     }  
  12.     gvOthrCntrctr.DataSource = dt;  
  13.     gvOthrCntrctr.DataBind();  
  14. }  
  15. protected void BtnShow_Click(object sender, EventArgs e) {  
  16.     for (int j = 0; j < gvOthrCntrctr.Rows.Count; j++) {  
  17.         TextBox txtCntrctrNmOthr = (TextBox) gvOthrCntrctr.Rows[j].Cells[1].FindControl("TxtOthrCntrctrNm");  
  18.         TextBox txtCntrctrAdhrOthr = (TextBox) gvOthrCntrctr.Rows[j].Cells[2].FindControl("TxtAadharNo");  
  19.         TextBox txtCntrctrPANOthr = (TextBox) gvOthrCntrctr.Rows[j].Cells[3].FindControl("TxtPANNo");  
  20.         TextBox txtCntrctrMobileNo = (TextBox) gvOthrCntrctr.Rows[j].Cells[3].FindControl("TxtOthrCntrctrMobileNo");  
  21.         TextBox txtCntrctrEmailId = (TextBox) gvOthrCntrctr.Rows[j].Cells[3].FindControl("TxtOthrCntrctrEmailid");  
  22.         Response.Write(txtCntrctrNmOthr.Text + " " + txtCntrctrAdhrOthr.Text.Trim() + " " + txtCntrctrPANOthr.Text + " " + txtCntrctrMobileNo.Text + " " + txtCntrctrEmailId.Text + "<br />");  
  23.     }  
  24. }   
On the click of top button "BtnAdd", after entering the number of Gridview rows in "TxtNo", we get the number of rows in the GridView. On click of the second button "BtnShow", it accesses the data in Gridview controls and shows them on the page. All the code is self explanatory. You can also download the attached solution from the link at the top of this blog.
 
That's all folks. Thank You!