title: asp.net gridview editindex
body: <p>Please take a look at this:</p>
  
  <pre><code>protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
  {
      int index = Convert.ToInt32(e.CommandArgument);
  
      if (e.CommandName == "Edit")
      {
          GridView1.EditIndex = index;
      }
  }
  </code></pre>
  
  <p>This code-snippet puts an entire gridview-row into the edit mode.</p>
  
  <p>But I need to put only the 3rd and 5th cell (suppose) of a row into edit mode.</p>
  
  <p>How can I do so?</p>
  
  <p>Some may suggest not using edit template. But I am only using code behind to manipulate my data in the gridview like the following:</p>
  
  <h2>aspx:</h2>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="GridView___Test._Default" %&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml" &gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;Untitled Page&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
  
          &lt;asp:GridView ID="GridView1" runat="server" Font-Names="Verdana" Font-Size="Small" AutoGenerateColumns="False" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowCommand="GridView1_RowCommand" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"&gt;            
          &lt;/asp:GridView&gt;
  
      &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <h2>code behind:</h2>
  
  <pre><code>public partial class _Default : System.Web.UI.Page
      {
          protected void Page_Load(object sender, EventArgs e)
          {
              if (!Page.IsPostBack)
              {
                  CreateGridView();
              }
          }
  
          private void CreateGridView()
          {
              GridView1.Columns.Clear();
  
              DataTable dataTable = Book.GetBooksDataSet().Tables[0];
  
              CommandField cf = new CommandField();
              cf.ShowEditButton = true;
  
              GridView1.Columns.Add(cf);
  
              foreach (DataColumn c in dataTable.Columns)
              {
                  BoundField boundField = new BoundField();
  
                  boundField.DataField = c.ColumnName;
                  boundField.HeaderText = c.ColumnName;
  
                  GridView1.Columns.Add(boundField);
              }
  
              GridView1.DataSource = dataTable;
              GridView1.DataBind();
          }
  
          protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
          {
              int index = Convert.ToInt32(e.CommandArgument);
  
              if (e.CommandName == "Edit")
              {
                  GridView1.EditIndex = index;
  
                  GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];
  
                  string id = selectedRow.Cells[1].Text;
                  string isbn = selectedRow.Cells[2].Text;
  
                  CreateGridView();
              }
              else if (e.CommandName == "Update")
              {
                  LinkButton updateButton = (LinkButton)e.CommandSource;
  
                  DataControlFieldCell dcfc = (DataControlFieldCell)updateButton.Parent;
  
                  GridViewRow gvr = (GridViewRow)dcfc.Parent;
  
                  ControlCollection cc = gvr.Cells[1].Controls;
  
                  TextBox tb = (TextBox)cc[0];
  
                  GridView1.EditIndex = -1;
  
                  CreateGridView();
              }
          }
  
          protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
          {
          }
  
          protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
          {
          }
  
          protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
          {            
          }
      }
  </code></pre>
  
  <p>And my view is to avoid asp.net scripting as much as possible.</p>
  
  <p>And also what is possible using scripts, must be possible by only code.</p>