Delete Selected Row in GridView Without Using SQL Database Connection GridView RowDeleting in Asp.Net C#

Delete Selected Row in GridView Without Using Database

Gridview  Selected Row Delete Gridview RowDeleting  Without DataBase Connection  for Temporary Table  Gridview in Asp.Net C#.

                            DEMO




                      HTML CODING


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" OnRowDeleting="GridView2_RowDeleting" >
            <AlternatingRowStyle BackColor="#FF9966" />
           <Columns>
              <asp:BoundField DataField="Sno" HeaderText="SNO" />
               <asp:BoundField DataField="Name" HeaderText="NAME" />
               <asp:BoundField DataField="City" HeaderText="CITY" />
               <asp:TemplateField HeaderText="Delete">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="delete">Delete</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
           </Columns>
        
             <HeaderStyle BackColor="#CC3300" />
            <RowStyle BackColor="Silver" />
        
             </asp:GridView>
   
    </div>
    </form>
</body>
</html>


                          C# CODING

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class RemoveRow : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }
    protected void Bind()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Sno", typeof(string));
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("City", typeof(string));

      
        dt.Rows.Add("1", "AAA", "Mumbai");
        dt.Rows.Add("2", "BBB", "New Delhi");
        dt.Rows.Add("3", "CCC", "Kolkata");
        dt.Rows.Add("4", "DDD", "Chennai");
        dt.Rows.Add("5", "EEE", "Bangalore");


        Session["Remove"] = dt;
        DataTable dt1 = (DataTable)Session["Remove"];
      
        GridView2.DataSource = dt;
        GridView2.DataBind();
    }
  
    protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        DataTable dt1 = (DataTable)Session["Remove"];
       // DataTable dt = new DataTable();
        if (dt1.Rows.Count > 0)
        {
            dt1.Rows[e.RowIndex].Delete();
            GridView2.DataSource = dt1;
            GridView2.DataBind();
        }
    }
}


First  - Add New Web Form - Select GridView From ToolBox & Bind The Fields






Next - Add  Column & Row To DataTable  & Pass Another DataTable








Next - GridView Deleting Row -  Delete Selected Row 







Next - Run[F5] - Delete GridView Selected Row







0 comments:

Post a Comment