Dynamic Gridview Bind without Database with delete functionality in asp.net c#

Many times we want to save the  values in dynamic way means we  do not want to save the values in data base and we want to delete particular row dynamically in grid view .

. Source Code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="dynamicallygridviewwithoutdatabase.aspx.cs" Inherits="dynamicallygridviewwithoutdatabase" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <fieldset style="width:450px;">
                <legend>Dynamic Gridview Bind without Database with delete functionality
                </legend>
           
            EmpID<br />
            <asp:TextBox ID="txtid" runat="server"></asp:TextBox>
            <br />
            Name
                <br /><asp:TextBox ID="txtname" runat="server"></asp:TextBox><br />
            Contact
                <br />
            <asp:TextBox ID="txtcontact" runat="server"></asp:TextBox><br />
            Email <br />
            <asp:TextBox ID="txtemail" runat="server"></asp:TextBox>
                <br />
                <br />

            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />
            <br />
            <br />
            <br />
            <asp:GridView ID="GridView1" runat="server" AutoGenerateDeleteButton="True" OnRowDeleting="GridView1_RowDeleting">
            </asp:GridView>
            </fieldset>
        </div>
    </form>
</body>
</html>

Code Behind(C#):-
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class dynamicallygridviewwithoutdatabase : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void createnewrow()
    {
        try
        {
        DataTable datatbl = new DataTable();
        if (ViewState["Row"] != null)
        {
            datatbl = (DataTable)ViewState["Row"];
            DataRow dr = null;
            if (datatbl.Rows.Count > 0)
            {
                dr = datatbl.NewRow();
                dr["empid"] = txtid.Text;
                dr["name"] = txtname.Text;
                dr["contact"] = txtcontact.Text;
                dr["emailid"] = txtemail.Text;
                datatbl.Rows.Add(dr);
                ViewState["Row"] = datatbl;
                GridView1.DataSource = ViewState["Row"];
                GridView1.DataBind();
            }
        }
        else
        {
            datatbl.Columns.Add("empid", typeof(int));
            datatbl.Columns.Add(new DataColumn("name", typeof(string)));
            datatbl.Columns.Add("contact", typeof(string));
            datatbl.Columns.Add("emailid", typeof(string));
           
            DataRow dr1 = datatbl.NewRow();
            dr1 = datatbl.NewRow();
            dr1["empid"] = txtid.Text;

            dr1["name"] = txtname.Text;
            dr1["contact"] = txtcontact.Text;
            dr1["emailid"] = txtemail.Text;
            datatbl.Rows.Add(dr1);
            ViewState["Row"] = datatbl;
            GridView1.DataSource = ViewState["Row"];
            GridView1.DataBind();
        }
            }
        catch(Exception ex)
        {

        }
    }



    protected void Button1_Click(object sender, EventArgs e)
    {
        createnewrow();

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

    {


        DataTable dt1 = (DataTable)ViewState["Row"];
       
        if (dt1.Rows.Count > 0)
        {
            dt1.Rows[e.RowIndex].Delete();
            GridView1.DataSource = dt1;
            GridView1.DataBind();
        }



   }
   
}

Out-Put:-




Dynamic Gridview Bind without Database with delete functionality in asp.net c# Dynamic Gridview Bind without Database with delete functionality in asp.net c# Reviewed by NEERAJ SRIVASTAVA on 2:35:00 PM Rating: 5

No comments:

Powered by Blogger.