Updating GridView in Asp.net 3.5 is Very Esey Let Do it
1: Program \Visual Studio 2008 and Create NewSite
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
10 Covert Boundfeild Columns into TemplateField
11 have Fun run Application
thank
By Usama Wahab khan
0 comments:
Post a Comment