Here i have explained how to add/delete row to GridView using Asp.Net C#.

HTML Markup

<asp:GridView runat="server" ID="gvDetails" ShowFooter="true" AllowPaging="true" PageSize="10" AutoGenerateColumns="false" ShowHeaderWhenEmpty="true" OnRowDeleting="gvDetails_RowDeleting">
<HeaderStyle CssClass="headerstyle" />
<Columns>
<asp:BoundField DataField="rowid" HeaderText="Row Id" ReadOnly="true" />
<asp:TemplateField HeaderText="Product Name">
<ItemTemplate>
<asp:TextBox ID="TxtName" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "Price">
<ItemTemplate>
<asp:TextBox ID="TxtPrice" runat="server" />
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="true" />
</Columns>
</asp:GridView>

Name Spaces

using System;
using System.Web.UI.WebControls;
using System.Data;

C#

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridview();
        }
    }
    protected void BindGridview()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("rowid", typeof(int));
        dt.Columns.Add("productname", typeof(string));
        dt.Columns.Add("price", typeof(string));
        DataRow dr = dt.NewRow();
        dr["rowid"] = 1;
        dr["productname"] = string.Empty;
        dr["price"] = string.Empty;
        dt.Rows.Add(dr);
        ViewState["Curtbl"] = dt;
        gvDetails.DataSource = dt;
        gvDetails.DataBind();
    }

    private void AddNewRow()
    {
        int rowIndex = 0;

        if (ViewState["Curtbl"] != null)
        {
            DataTable dt = (DataTable)ViewState["Curtbl"];
            DataRow drCurrentRow = null;
            if (dt.Rows.Count > 0)
            {
                for (int i = 1; i <= dt.Rows.Count; i++)
                {
                    TextBox Txtname = (TextBox)gvDetails.Rows[rowIndex].Cells[1].FindControl("TxtName");
                    TextBox Txtprice = (TextBox)gvDetails.Rows[rowIndex].Cells[2].FindControl("TxtPrice");
                    drCurrentRow = dt.NewRow();
                    drCurrentRow["rowid"] = i + 1;
                    dt.Rows[i - 1]["productname"] = Txtname.Text;
                    dt.Rows[i - 1]["price"] = Txtprice.Text;
                    rowIndex++;
                }
                dt.Rows.Add(drCurrentRow);
                ViewState["Curtbl"] = dt;
                gvDetails.DataSource = dt;
                gvDetails.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState Value is Null");
        }
        SetOldData();
    }
    private void SetOldData()
    {
        int rowIndex = 0;
        if (ViewState["Curtbl"] != null)
        {
            DataTable dt = (DataTable)ViewState["Curtbl"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TextBox Txtname = (TextBox)gvDetails.Rows[rowIndex].Cells[1].FindControl("TxtName");
                    TextBox Txtprice = (TextBox)gvDetails.Rows[rowIndex].Cells[2].FindControl("TxtPrice");
                    Txtname.Text = dt.Rows[i]["productname"].ToString();
                    Txtprice.Text = dt.Rows[i]["price"].ToString();
                    rowIndex++;
                }
            }
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        AddNewRow();
    }
    protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if (ViewState["Curtbl"] != null)
        {
            DataTable dt = (DataTable)ViewState["Curtbl"];
            DataRow drCurrentRow = null;
            int rowIndex = Convert.ToInt32(e.RowIndex);
            if (dt.Rows.Count > 1)
            {
                dt.Rows.Remove(dt.Rows[rowIndex]);
                drCurrentRow = dt.NewRow();
                ViewState["Curtbl"] = dt;
                gvDetails.DataSource = dt;
                gvDetails.DataBind();

                for (int i = 0; i < gvDetails.Rows.Count - 1; i++)
                {
                    gvDetails.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                }
                SetOldData();
            }
        }
    }

Screenshot

Add Comments