title: How to Update ASP.NET GridView Row with ObjectDataSource with Code Behind Programmatically
body: <p>I am having problem with implementing editable GridView using ObjectDataSource on CodeBehind; I wonder what missing pieces I need to add in my code behind (events): </p>
  
  <p><strong>ASPX Code:</strong></p>
  
  <pre><code>&lt;asp:GridView ID="grdPlayer" runat="server" AutoGenerateColumns="False" 
              AutoGenerateEditButton="true" AutoGenerateDeleteButton="true" 
              onrowediting="grdPlayer_RowEditing" 
              onrowcancelingedit="grdPlayer_RowCancelingEdit" 
  onrowupdating="grdPlayer_RowUpdating"&gt;
            &lt;Columns&gt;
                  &lt;asp:BoundField DataField="ID" Visible="false"/&gt;
                  &lt;asp:BoundField DataField="FirstName" HeaderText="First Name" 
                      SortExpression="FirstName" /&gt;  
                  &lt;asp:BoundField DataField="LastName" HeaderText="LastName"   
                      SortExpression="LastName" /&gt;  
                  &lt;asp:BoundField DataField="Age" HeaderText="Age"   
                      SortExpression="Age" /&gt;  
  
            &lt;/Columns&gt;  
  
  
          &lt;/asp:GridView&gt;
  </code></pre>
  
  <p><strong>ASPX.CS Code Behind:</strong></p>
  
  <pre><code>  using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Web.UI;
      using System.Web.UI.WebControls;
      using System.Data;
  
      public partial class _Default : System.Web.UI.Page
      {
          ObjectDataSource dataSource = new ObjectDataSource();
  
          protected void Page_Load(object sender, EventArgs e)
          {
              if (!IsPostBack)
              {
  
                  dataSource.TypeName = "Cricket.CricketBL";
                  dataSource.UpdateMethod = "UpdatePlayer";
                  Parameter p1 = new Parameter("ID", DbType.Int32);
                  Parameter p2 = new Parameter("FName", DbType.String);
                  Parameter p3 = new Parameter("LName", DbType.String);
                  Parameter p4 = new Parameter("Age", DbType.Int32);
                  dataSource.UpdateParameters.Add(p1);
                  dataSource.UpdateParameters.Add(p2);
                  dataSource.UpdateParameters.Add(p3);
                  dataSource.UpdateParameters.Add(p4);
  
                  bindGridView();
  
              }
          }
  
          #region Grid Events
          protected void grdPlayer_RowEditing(object sender, GridViewEditEventArgs e)
          {
              grdPlayer.EditIndex = e.NewEditIndex;
          }
  
          protected void grdPlayer_RowUpdating(object sender, GridViewUpdateEventArgs e)
          {
              grdPlayer.EditIndex = -1;
              bindGridView();
  
  
          }
  
  
  
          protected void grdPlayer_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
          {
              grdPlayer.EditIndex = -1;
              bindGridView();
          }
          #endregion
  
          void bindGridView()
          {
              dataSource.SelectMethod = "GetAllPlayer";
              dataSource.TypeName = "Cricket.CricketBL";
              grdPlayer.DataSource = dataSource;
              grdPlayer.DataBind();
          }
      }
  </code></pre>
  
  <ol>
  <li>Where should I put UpdateMethod code (currently in Page_Load)?</li>
  <li>Which events should I implement in code behind so that my edited grid row calls UpdateMethod with correct NewValues?</li>
  <li>When I click on Edit link it does not render GridView in Edit mode (text boxes,etc); I have to click it twice to do so; why?</li>
  <li>BIG QUESTION: Clicking on Update link does not invoke ObjectDataSource UpdateMethod?</li>
  </ol>