Saturday 18 April 2015

Update,Delete GridView Selected RowUpdating Without Using Sql DataBase Connection in Asp.Net C#

Update,Delete GridView RowUpdating Without Using Sql DataBase Connection

Update & Delete GridView RowUpdating  Temporary DataTable Values Without Using DataDase Connection 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>
   
   <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" OnRowDeleting="GridView2_RowDeleting" Height="400px" OnRowCancelingEdit="GridView2_RowCancelingEdit" OnRowEditing="GridView2_RowEditing" OnRowUpdating="GridView2_RowUpdating" Width="500px" >
            <AlternatingRowStyle BackColor="#FF9966" />
           <Columns>
               <asp:TemplateField HeaderText="SNO">
                   <ItemTemplate>
                       <asp:Label ID="Label1" runat="server" Text='<%# Eval("Sno"%>'></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="NAME">
                   <ItemTemplate>
                       <asp:Label ID="Label2" runat="server" Text='<%# Eval("Name"%>'></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="CITY">
                   <EditItemTemplate>
          <asp:TextBox ID="txtCtyUpdate" runat="server"></asp:TextBox>
                   </EditItemTemplate>
                   <ItemTemplate>
                       <asp:Label ID="Label3" runat="server" Text='<%# Eval("City"%>'></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="Delete">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="delete">Delete</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
              <asp:CommandField HeaderText="Edit" ShowEditButton="True" />
           </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["Upd"] = dt;
        DataTable dt1 = (DataTable)Session["Upd"];
      
        GridView2.DataSource = dt;
        GridView2.DataBind();
    }
  
    protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        DataTable dt1 = (DataTable)Session["Upd"];
       // DataTable dt = new DataTable();
        if (dt1.Rows.Count > 0)
        {
            dt1.Rows[e.RowIndex].Delete();
            GridView2.DataSource = dt1;
            GridView2.DataBind();
        }
    }
    protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {       
        TextBox txtCity = (TextBox)GridView2.Rows[e.RowIndex].Cells[3].FindControl("txtCtyUpdate");

        DataTable dt1 = (DataTable)Session["Upd"];
    
         //  <----Integer value Update Below Method---->

       // int number = Convert.ToInt32(txtQty.Text);

      //  dt1.Rows[e.RowIndex]["City"] = number;

        dt1.Rows[e.RowIndex]["City"] = txtCity.Text;      

        dt1.AcceptChanges();

        GridView2.EditIndex = -1;
        GridView2.DataSource = dt1;
        GridView2.DataBind();
    }
    protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView2.EditIndex = -1;
        DataTable dt1 = (DataTable)Session["Upd"];
        GridView2.DataSource = dt1;
        GridView2.DataBind();
    }
protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
    {
        DataTable dt1 = (DataTable)Session["Upd"];
        GridView2.EditIndex = e.NewEditIndex;
        GridView2.DataSource = dt1;
        GridView2.DataBind();
    }

}










































0 comments:

Post a Comment