RowState of Gridview doesnot point to correct row
<p>check any check box and then click on the "Button1" . in response it will display the id of checked item , but in the following code id is not correct. please tell me what is going wrong in my code, please have a look at the following code.
<strong>test.aspx.cs</strong> is as follows</p>

<pre><code>using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write("Aler id are =");
    foreach (GridViewRow row in TaskGridView.Rows)
    {
        if (row.RowState == DataControlRowState.Alternate)
        {

            Response.Write("," +row.Cells[1].Text);// id

         }
    }
    }
}
</code></pre>

<p><strong>test.aspx is as follow</strong></p>

<pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %&gt;

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

&lt;%@ Import Namespace="System.Data" %&gt;


&lt;script runat="server"&gt;

  protected void Page_Load(object sender, EventArgs e)
  {

    if (!Page.IsPostBack)
    {
      // Create a new table.
      DataTable taskTable = new DataTable("TaskList");

      // Create the columns.
      taskTable.Columns.Add("Id", typeof(int));
      taskTable.Columns.Add("Description", typeof(string));
      taskTable.Columns.Add("IsComplete", typeof(bool) );

      //Add data to the new table.
      for (int i = 0; i &lt; 20; i++)
      {
        DataRow tableRow = taskTable.NewRow();
        tableRow["Id"] = i;
        tableRow["Description"] = "Task " + i.ToString();
        tableRow["IsComplete"] = false;            
        taskTable.Rows.Add(tableRow);
      }

      //Persist the table in the Session object.
      Session["TaskTable"] = taskTable;

      //Bind data to the GridView control.
      BindData();
    }

  }

  protected void TaskGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {
    TaskGridView.PageIndex = e.NewPageIndex;
    //Bind data to the GridView control.
    BindData();
  }

  protected void TaskGridView_RowEditing(object sender, GridViewEditEventArgs e)
  {
    //Set the edit index.
    TaskGridView.EditIndex = e.NewEditIndex;
    //Bind data to the GridView control.
    BindData();
  }

  protected void TaskGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
  {
    //Reset the edit index.
    TaskGridView.EditIndex = -1;
    //Bind data to the GridView control.
    BindData();
  }

  protected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
  {    
    //Retrieve the table from the session object.
    DataTable dt = (DataTable)Session["TaskTable"];

    //Update the values.
    GridViewRow row = TaskGridView.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.
    TaskGridView.EditIndex = -1;

    //Bind data to the GridView control.
    BindData();
  }

  private void BindData()
  {
    TaskGridView.DataSource = Session["TaskTable"];
    TaskGridView.DataBind();
  }
&lt;/script&gt;

&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head id="Head1" runat="server"&gt;
    &lt;title&gt;GridView example&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;form id="form1" runat="server"&gt;
    &lt;div&gt;

        &lt;asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /&gt;

      &lt;asp:GridView ID="TaskGridView" runat="server" 
        AutoGenerateEditButton="True" 
        AllowPaging="true"
        OnRowEditing="TaskGridView_RowEditing"         
        OnRowCancelingEdit="TaskGridView_RowCancelingEdit" 
        OnRowUpdating="TaskGridView_RowUpdating"
        OnPageIndexChanging="TaskGridView_PageIndexChanging"&gt;
      &lt;/asp:GridView&gt;

    &lt;/div&gt;
    &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
