Thursday, 1 November 2012

Dynamically Add and Remove Rows In GridView In Asp.Net


.aspx code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DynamicGridview.aspx.cs" Inherits="DynamicGridview" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <center>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
            ShowFooter="true" onrowcommand="GridView1_RowCommand">
        <AlternatingRowStyle BackColor="#CCCCFF" BorderColor="#003366"
            BorderStyle="Solid" BorderWidth="1px" />
    <Columns>
    <asp:TemplateField HeaderText="Name">
    <ItemTemplate>
    <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Phone">
    <ItemTemplate>
    <asp:TextBox ID="txtphone" runat="server"></asp:TextBox>
    </ItemTemplate>
    <FooterTemplate>
    <center>
    <asp:Button ID="btnadd" runat="server" Text="Add New" CommandName="addnew" />
    </center>
    </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Email">
    <ItemTemplate>
    <asp:TextBox ID="txtemail" runat="server"></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Address">
    <ItemTemplate>
    <asp:TextBox ID="txtaddress" runat="server"></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Delete">
    <ItemTemplate>
    <asp:Button ID="btndelete" runat="server" Text="Delete" CommandName="deleted" />
    </ItemTemplate>
    </asp:TemplateField>             
    </Columns>
        <EmptyDataRowStyle BackColor="White" />
        <FooterStyle BackColor="#336699" />
        <HeaderStyle BackColor="#336699" ForeColor="White" />
    </asp:GridView>
    </center>
    </div>
    </form>
</body>
</html>

.cs Code
--------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

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

    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
       
        if (e.CommandName == "addnew")
        {
            addnewRow();
         
        }

        if (e.CommandName == "deleted")
        {
            Button btn = (Button)e.CommandSource;
            if (btn != null)
            {
                GridViewRow grdrow = (GridViewRow) btn.NamingContainer;
                int row = grdrow.RowIndex;
                deleteRow();
                DataTable dtt = (DataTable)ViewState["deleteRow"];

                if (dtt.Rows.Count > 1)
                {
                    dtt.Rows.RemoveAt(row);
                    dtt.AcceptChanges();
                }

                GridView1.DataSource = dtt;
                GridView1.DataBind();
               
            }

        }
    }
    public void bind_grid()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("Name", typeof(string)));
        dt.Columns.Add(new DataColumn ("Phone",typeof(string)));
        dt.Columns.Add(new DataColumn("Email", typeof(string)));
        dt.Columns.Add(new DataColumn("Address",typeof(string)));

        DataRow dr = dt.NewRow();
      
        dt.Rows.Add(dr);
        ViewState["CurrentTable"] = dt;
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    public void addnewRow()
    {
        DataTable dt1 = new DataTable();
        dt1.Columns.Add(new DataColumn("Name", typeof(string)));
        dt1.Columns.Add(new DataColumn("Phone", typeof(string)));
        dt1.Columns.Add(new DataColumn("Email", typeof(string)));
        dt1.Columns.Add(new DataColumn("Address", typeof(string)));

        for (int i = 0; i < GridView1.Rows.Count;i++ )
        {
            TextBox txt_name = (TextBox)GridView1.Rows[0].Cells[0].FindControl("txtname");
            TextBox txt_phone = (TextBox)GridView1.Rows[0].Cells[0].FindControl("txtphone");
            TextBox txt_email = (TextBox)GridView1.Rows[0].Cells[0].FindControl("txtemail");
            TextBox txt_address = (TextBox)GridView1.Rows[0].Cells[0].FindControl("txtaddress");

            DataRow dr = dt1.NewRow();
            dr["Name"] = txt_name.Text;
            dr["Phone"] = txt_phone.Text;
            dr["Email"] = txt_email.Text;
            dr["Address"] = txt_address.Text;
            dt1.Rows.Add(dr);
            dt1.AcceptChanges();
        }
        DataRow dr1 = dt1.NewRow();
        dr1["Name"] = string.Empty;
        dr1["Phone"] = string.Empty;
        dr1["Email"] = string.Empty;
        dr1["Address"] = string.Empty;

        dt1.Rows.Add(dr1);
        ViewState["deleteRow"] = dt1;
        GridView1.DataSource = dt1;
        GridView1.DataBind();
    }

    public void deleteRow()
    {
        DataTable dt1 = new DataTable();
        dt1.Columns.Add(new DataColumn("Name", typeof(string)));
        dt1.Columns.Add(new DataColumn("Phone", typeof(string)));
        dt1.Columns.Add(new DataColumn("Email", typeof(string)));
        dt1.Columns.Add(new DataColumn("Address", typeof(string)));

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            TextBox txt_name = (TextBox)GridView1.Rows[0].Cells[0].FindControl("txtname");
            TextBox txt_phone = (TextBox)GridView1.Rows[0].Cells[0].FindControl("txtphone");
            TextBox txt_email = (TextBox)GridView1.Rows[0].Cells[0].FindControl("txtemail");
            TextBox txt_address = (TextBox)GridView1.Rows[0].Cells[0].FindControl("txtaddress");

            DataRow dr = dt1.NewRow();
            dr["Name"] = txt_name.Text;
            dr["Phone"] = txt_phone.Text;
            dr["Email"] = txt_email.Text;
            dr["Address"] = txt_address.Text;
            dt1.Rows.Add(dr);
            dt1.AcceptChanges();
        }
      
        ViewState["deleteRow"] = dt1;
        GridView1.DataSource = dt1;
        GridView1.DataBind();
    }
}

No comments:

Post a Comment