CRUD Operation in GridView in ASP.NET (Add/Update/Delete) Programmetrically

No.of Views7769
Bookmarked0 times
Downloads 
Votes0
By  usamawahabkhan   On  14 May 2010 09:05:06
Tag : ASP.NET , Grid Controls
CRUD Operation in GridView in ASP.NET(Add/Update/Delete) Programmetrically
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this article i have shared with you CRUD operation with Grdiview. 

Image Loading

Updating GridView in Asp.net 3.5 is Very Esey Let Do it.

Steps by Steps

1: Program \Visual Studio 2008 and Create NewSite 

Image Loading

2: Drag n Drop GridView on Form 

Image Loading

3. Right Click on Gridview and Select Properties and in properties Window Select Event 

Image Loading

4 double click on form and past on on load Event

if (IsPostBack != true)
{
OleDbDataAdapter da = new OleDbDataAdapter("Select * from MapLayers", cn);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}

5    gridview updating event past this code

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string layername = null;
string LayerId = null;
try
{
OleDbDataAdapter da = new OleDbDataAdapter("Select * from MapLayers", cn);
DataSet ds = new DataSet();
da.Fill(ds);
DataRow row = ds.Tables[0].NewRow();
row[0] = ((TextBox) GridView1.Rows[e.RowIndex].FindControl("Textbox1")).Text;
row[1]= ((Label)GridView1.Rows[e.RowIndex].FindControl("Label1")).Text;
ds.Tables[0].Rows[e.RowIndex][0] = row[0];
Response.Write(((TextBox)GridView1.Rows[e.RowIndex].FindControl("Textbox1")).Text.ToString());
OleDbCommandBuilder builder = new OleDbCommandBuilder(da);
da.Update(ds.GetChanges());
ds.AcceptChanges();
GridView1.EditIndex = -1;
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
ClientScript.RegisterStartupScript(GetType(), "Message", "");
}
catch { }
}

6    add code on gridview Editing event

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
}
7 add code on gridview Deleting Event
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
OleDbDataAdapter da = new OleDbDataAdapter("Select * from MapLayers", cn);
DataSet ds = new DataSet();
da.Fill(ds);
DataRow row = ds.Tables[0].NewRow();
ds.Tables[0].Rows[e.RowIndex].Delete();
OleDbCommandBuilder builder = new OleDbCommandBuilder(da);
da.Update(ds.GetChanges());
ds.AcceptChanges();
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
GridView1.EditIndex = -1;
//Reset Edit Index
ClientScript.RegisterStartupScript(GetType(), "Message", "");
}

 

8. Set GridView autogenerateColumns propertie to false

9 add to 3Colums in GridView

Column 1 CommandColumn

Column 2 boundfield

Column 3 boundfield

 

Image Loading

10 Covert Boundfeild Columns into TemplateField

11 have Fun run Application

Sample Project Source

Download source files -29 kb

 
Sign Up to vote for this article
 
About Author
 
usamawahabkhan
Occupation-Not Provided
Company-Not Provided
Member Type-Senior
Location-Pakistan
Joined date-06 May 2010
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top