Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.67/5 (2 votes)
See more:
Hai i need to add new row in gridview with textbox and dropdownlist when user clicks

the button in gridview.


please some one help me for getting this solution.
Posted
Comments
Prasad_Kulkarni 8-May-12 9:00am    
Glad it helps!
Thank you for 'Accepting Solution'

 
Share this answer
 
Comments
VJ Reddy 22-May-12 6:25am    
Good references. 5!
sravani.v 22-May-12 6:38am    
Thank you Reddy
Try this:
This example shows how to generate a Row in GridView with TextBoxes when clicking a Button.
Aspx page:
ASP.NET
<asp:gridview ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false">
        <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" />
            </FooterTemplate>
        </asp:TemplateField>
        </Columns>
</asp:gridview>


Code behind:
C#
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);
        //dr = dt.NewRow();
 
        //Store the DataTable in ViewState
        ViewState["CurrentTable"] = dt;
 
        Gridview1.DataSource = dt;
        Gridview1.DataBind();
    }


Page load:
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            SetInitialRow(); 
        }
    }


[EDIT]
For more have look on following links:
Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality[^]
Save and Retrieve Dynamic TextBox values in GridView to SQL Server Database[^]
ADDING DYNAMIC ROWS IN GRIDVIEW WITH TEXTBOXES[^]

Hope it helps!
 
Share this answer
 

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