Resources » Code Snippets » ASP.NET GridView

Bind Gridview from Session , Then Edit, Update, Delete the gridview


Posted Date: 24-Aug-2008  Last Updated:   Category: ASP.NET GridView    
Author: Member Level: Gold    Points: 10



How To Store Data In a Session Datatable ? Add, Edit Update and Delete Data ?

See the Detailed Answer for all of your questions related to Asp.net Gridview control , In terms of C#.

Here I am Storing a Datatable in Session ( without Database ) and doing Edit, Update and Delete operations on it.. Adding values from another page called Webform1.aspx and the Gridview1 is located in the page caled default.aspx

See the C# CODES


1) WebForm1.aspx.cs


using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;

namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = "";
TextBox2.Text = "";
}

protected void Button2_Click(object sender, EventArgs e)
{// ADDED DATA TO THE TEXT BOX AND CLICKED ON ADD BUTTON
AddNewData();
}

protected void Button1_Click(object sender, EventArgs e)
{// GOING TO ANOTHER FORM TO DISPLAY THE DATA
Response.Redirect("Default.aspx");
}
public void AddNewData()
{// ADDING DATA AND STORING THE DATATABLE INTO THE SESSION
DataTable dt = new DataTable();

if (Session["myDatatable"] != null)
{
dt = (DataTable)Session["myDatatable"];
}
else
{
dt.Columns.Add("Name");
dt.Columns.Add("ID");
}

DataRow drow = dt.NewRow();

drow["Name"] = TextBox2.Text.ToString();
drow["ID"] = TextBox1.Text.ToString();

dt.Rows.Add(drow);

Session["myDatatable"] = dt;
TextBox1.Text = "";
TextBox2.Text = "";
}

}
}




2) Default.aspx.cs Here we have a gridview called Gridview1 to show,edit and delete data

I was added two asp textboxes to the EditItemTemplate of the gridview, One for the ID field and another for the Name field they are named TextBox1 ( In the ID edit template) and Textbox4 (In the Name Edit Tempate) Respectively, Also added Command Fields to the Gridview1 ( Gridview1 > Properties > Columns > Command Fileds > Edit , Delete )



using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;

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

protected void Button1_Click(object sender, EventArgs e)
{// CLICKED ON THE BUTTON TO GO TO THE PAGE WITH TEXTBOXES TO ADD NEW DATA
Response.Redirect("Webform1.aspx");
}

public void BindMyGridview()
{// BINDING THE Gridview1 FROM SESSION
if (Session["myDatatable"] != null)
{
DataTable dt = (DataTable)Session["myDatatable"];

if ((dt != null) && (dt.Rows.Count > 0))
{
GridView1.Visible = true;
GridView1.DataSource = dt;
GridView1.DataBind();
}
else
{
GridView1.Visible = false;
}
}

}


protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{// Gridview1's RowEditing Event
GridView1.EditIndex = e.NewEditIndex;
BindMyGridview();
}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{// Gridview1's RowUpdating Event

TextBox TextBoxWithID = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2");
TextBox TextBoxWithName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox4");
string NewID = TextBoxWithID.Text.ToString();
string NewName = TextBoxWithName.Text.ToString();

DataTable dt = (DataTable)Session["myDatatable"];

DataRow dr = dt.Rows[e.RowIndex];

dr["ID"] = NewID;
dr["Name"] = NewName;
dr.AcceptChanges();
Session["myDatatable"] = dt;

GridView1.EditIndex = -1;
BindMyGridview();

}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{// Gridview1's RowEditing Cancel Event
GridView1.EditIndex = -1;
BindMyGridview();
}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{// Gridview1's RowDeleting Event
DataTable dt = (DataTable)Session["myDatatable"];

DataRow dr = dt.Rows[e.RowIndex];

dt.Rows.Remove(dr);

GridView1.EditIndex = -1;
BindMyGridview();
}
}
}




Happy Asp.net C# coding

Thanks

Mathew Philip


Did you like this resource? Share it with your friends and show your love!

Responses to "Bind Gridview from Session , Then Edit, Update, Delete the gridview"
Author: Raj Kumar Prajapati    27 Aug 2008Member Level: Bronze   Points : 0
ITS WORKING
THANKS
RAJ PRAJAPATI



Author: priya    08 Nov 2008Member Level: Bronze   Points : 0



upload.aspx.txt
Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    Today
      Last 7 Daysmore...

      Awards & Gifts
      Talk to Webmaster Tony John
      Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India