Gridview RowEditing event in Asp.net

       The RowEditing event is raised when a row's Edit button is clicked, but before the GridView control enters edit mode. This enables you to provide an event-handling method that performs a custom routine,
such as canceling the edit operation, whenever this event occurs.

A GridViewEditEventArgs object is passed to the event-handling method, which enables you to determine the index of the current row and to indicate that the edit operation should be canceled. To cancel the edit operation, set the Cancel property of the GridViewEditEventArgs object to true.

The following example demonstrates how to use the RowEditing event to put a row in edit mode when the data source is set programmatically.

 Write the following code in aspx design page:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>GridView example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
      <asp:GridView ID="GridView1" runat="server" 
        AutoGenerateEditButton="True" 
        AllowPaging="true"
        OnRowEditing="GridView1_RowEditing"         
        OnRowCancelingEdit="GridView1_RowCancelingEdit" 
        OnRowUpdating="GridView1_RowUpdating"
        OnPageIndexChanging="GridView1_PageIndexChanging">
      </asp:GridView>
 
    </div>
    </form>
</body>
</html>
 
write the following code in aspx.cs code behind side:
 
  protected void Page_Load(object sender, EventArgs e)
  {
 
    if (!Page.IsPostBack)
    {
      // Create a new table.
      DataTable Table1 = new DataTable("dataTable");
 
      // Create the columns.
      Table1 .Columns.Add("Id", typeof(int));
      Table1 .Columns.Add("Description", typeof(string));
      Table1 .Columns.Add("IsComplete", typeof(bool) );
 
      //Add data to the new table.
      for (int i = 0; i < 20; i++)
      {
        DataRow tableRow = Table1 .NewRow();
        tableRow["Id"] = i;
        tableRow["Description"] = "number" + i.ToString();
        tableRow["IsComplete"] = false;            
        Table1 .Rows.Add(tableRow);
      }
 
      //Persist the table in the Session object.
      Session["dataTable"] = Table1 ;
 
      //Bind data to the GridView control.
      BindData();
    }
 
  }
 
  protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {
    GridView1.PageIndex = e.NewPageIndex;
    //Bind data to the GridView control.
    BindData();
  }
 
  protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
  {
    //Set the edit index.
    GridView1.EditIndex = e.NewEditIndex;
    //Bind data to the GridView control.
    BindData();
  }
 
  protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
  {
    //Reset the edit index.
    GridView1.EditIndex = -1;
    //Bind data to the GridView control.
    BindData();
  }
 
  protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
  {    
    //Retrieve the table from the session object.
    DataTable dt = (DataTable)Session["dataTable"];
 
    //Update the values.
    GridViewRow row = GridView1.Rows[e.RowIndex];
    dt.Rows[row.DataItemIndex]["Id"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
    dt.Rows[row.DataItemIndex]["Description"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
    dt.Rows[row.DataItemIndex]["IsComplete"] = ((CheckBox)(row.Cells[3].Controls[0])).Checked;
 
    //Reset the edit index.
    GridView1.EditIndex = -1;
 
    //Bind data to the GridView control.
    BindData();
  }
 
  private void BindData()
  {
    GridView1.DataSource = Session["dataTable"];
    GridView1.DataBind();
  } 

7 comments:

  1. Replies
    1. .Net Tutorial: Gridview Rowediting Event In Asp.Net >>>>> Download Now

      >>>>> Download Full

      .Net Tutorial: Gridview Rowediting Event In Asp.Net >>>>> Download LINK

      >>>>> Download Now

      .Net Tutorial: Gridview Rowediting Event In Asp.Net >>>>> Download Full

      >>>>> Download LINK 9B

      Delete
  2. Lose the background image. I can't see a thing on the page.

    ReplyDelete
  3. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowediting(v=vs.110).aspx

    ReplyDelete
  4. .Net Tutorial: Gridview Rowediting Event In Asp.Net >>>>> Download Now

    >>>>> Download Full

    .Net Tutorial: Gridview Rowediting Event In Asp.Net >>>>> Download LINK

    >>>>> Download Now

    .Net Tutorial: Gridview Rowediting Event In Asp.Net >>>>> Download Full

    >>>>> Download LINK

    ReplyDelete