Updating GridView in Asp.net 3.5 is Very Esey Let Do it
1: Program \Visual Studio 2008 and Create NewSite
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi-JohAqinsYFHm00fHfDApkJeH8dbR2Ngw8Ge_40JKaqXdaGnViymrPZtad2alWBtEmCPpa9A4aQw3CbCp5s9iap3LtVlVs4S5w1yKd4YczDdYnnWX2DRliRSvjIQUht2yG-CGn5ulwK1j/s400/2.bmp)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1Y2sveXxg3hsaW1jDUezbghJrB3CS00_2AIjcHJEcn8Y2KRUeBeNuumt76vHKvgYy8UT7gdHN6nYZRSEKqxnaT8lKze6Wpk9h2oUHWlshXo0W18gdcssauaLTqI7OXRXhiGWuV4A6DiqH/s400/untitled3.bmp)
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 and on 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 { }
}
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
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlNzHbNd3t-vzb22JTuJ_gAzkiHaaBByvEssTB_Vn7D2XsaaLwAbDwh0EXwEJcAk6ae_HzreOfIaf6mj1feDp33pVdp_kQPG6QUm9EvWTsH-YknISJVjR03P2TkHPiSP7HPTUs1zUdgixz/s400/untitled2.bmp)
10 Covert Boundfeild Columns into TemplateField
11 have Fun run Application
thank
By Usama Wahab khan
0 comments:
Post a Comment