gridview add new row

   In this article I will add new row to grid. Trick is very simple.
we have footer template for each Template, by default the visibility of this footer template is set to false.

On add new row button click event, we will set the visibility of Gridview footer row to true and bind the grid again.

To stop page being refreshed, add this gridview into AjaxUpdate panel.


 

Aspx code:

font face="tahoma,arial,helvetica,sans-serif" size="2">      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:TemplateField HeaderText="ID">
                    <ItemTemplate>
                        <asp:Label ID="lblID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"ID") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtID" runat="server"></asp:TextBox>
                    </FooterTemplate>
                  
                </asp:TemplateField>
                <asp:TemplateField HeaderText="First Name">
                    <ItemTemplate>
                        <asp:Label ID="lblfirstName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"FirstName") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Last Name">
                    <ItemTemplate>
                        <asp:Label ID="lblLastName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"LastName") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Email">
                    <ItemTemplate>
                        <asp:Label ID="lblEmail" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Email") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Check all">
                    <HeaderTemplate>
                        <asp:CheckBox ID="CheckBox2" AutoPostBack="true" runat="server"  OnCheckedChanged="CheckBox2_CheckedChanged1" />
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1"  runat="server" />
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:LinkButton ID="lnkSave" runat="server" CommandName="Save" OnClick="lnkSave_Click">Save</asp:LinkButton>
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

C# code behind

  using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
           BindGrid();
        }
    }

    private void BindGrid()
    {
         GridView1.DataSource = GridDataProvider.GetData();
            GridView1.DataBind();
    }


    protected void CheckBox2_CheckedChanged1(object sender, EventArgs e)
    {
        foreach (GridViewRow r in GridView1.Rows)
        {
            ((CheckBox)r.FindControl("CheckBox1")).Checked = true;
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        GridView1.ShowFooter = true;
        BindGrid();
    }
    protected void lnkSave_Click(object sender, EventArgs e)
    {
        string ID = (  (TextBox)  GridView1.FooterRow.FindControl("txtID") ).Text ;
        string firstName = (  (TextBox)  GridView1.FooterRow.FindControl("txtFirstName") ).Text;

        // similarly you can find other controls and save

    }
}


Vb.Net Code

  Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

Public Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs)
  If Not Page.IsPostBack Then
   BindGrid()
  End If
End Sub

Private Sub BindGrid()
  GridView1.DataSource = GridDataProvider.GetData()
  GridView1.DataBind()
End Sub


Protected Sub CheckBox2_CheckedChanged1(sender As Object, e As EventArgs)
  For Each r As GridViewRow In GridView1.Rows
   (DirectCast(r.FindControl("CheckBox1"), CheckBox)).Checked = True
  Next

End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs)

End Sub
Protected Sub Button2_Click(sender As Object, e As EventArgs)
  GridView1.ShowFooter = True
  BindGrid()
End Sub
Protected Sub lnkSave_Click(sender As Object, e As EventArgs)
  Dim ID As String = (DirectCast(GridView1.FooterRow.FindControl("txtID"), TextBox)).Text
  Dim firstName As String = (DirectCast(GridView1.FooterRow.FindControl("txtFirstName"), TextBox)).Text

  ' similarly you can find other controls and save

End Sub
End Class

'=======================================================
'Service provided by Telerik (
www.telerik.com)
'Conversion powered by NRefactory.
'Built and maintained by Todd Anglin and Telerik
'=======================================================

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.