title: How to Store SelectedRow.Cell[0] value in a session when CommandField is "Select" in a GridView
body: <p>How to Store <code>SelectedRow.Cell[0]</code> value in a session when CommandField is "Select" in a GridView. ? What the changes i will do in my code so that when a Select Command executes on my grid (By Clicking on Select Link of my grid), It will Store the <code>NatureOfWorkID</code> of that row in the session variable. Below is my code. Please guide me that what changes required in the below code so that when ever some one will click on Select, the natureofworkID of that row will be stored in a Session and i will bind another grid from the session variable as per my requirement.</p>
  
  <pre><code> &lt;asp:GridView ID="GVNatureOFWork" runat="server" AllowPaging="true" ShowFooter="true" PageSize="10" AutoGenerateColumns="false"
      DataKeyNames="NatureOfWorkID"
       OnPageIndexChanging="GVNatureOFWork_PageIndexChanging" 
       onrowcancelingedit="GVNatureOFWork_RowCancelingEdit" 
       onrowcommand="GVNatureOFWork_RowCommand" onrowdeleting="GVNatureOFWork_RowDeleting" 
       onrowediting="GVNatureOFWork_RowEditing" onrowupdating="GVNatureOFWork_RowUpdating" 
       OnSelectedIndexChanged="GVNatureOFWork_SelectedIndexChanged" OnRowDataBound="GVNatureOFWork_RowDataBound"
       HeaderStyle-BackColor="BlueViolet" 
       HeaderStyle-ForeColor="White" BackColor="White"&gt;
          &lt;Columns&gt;
              &lt;asp:TemplateField HeaderText="NatureOfWorkID"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="lblNautureOfWorkID" runat="server" Text='&lt;%#Eval("NatureOfWorkID") %&gt;' /&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
  
              &lt;asp:TemplateField HeaderText="Nature of Work Name"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="lblNatureOfWorkName" runat="server" Text='&lt;%#Eval("NatureOfWorkName") %&gt;' /&gt;
                  &lt;/ItemTemplate&gt;
                  &lt;EditItemTemplate&gt;
                      &lt;asp:TextBox ID="txtNatureOfWorkName" runat="server" Text='&lt;%#Eval("NatureOfWorkName") %&gt;' /&gt;
                  &lt;/EditItemTemplate&gt;
                  &lt;FooterTemplate&gt;
                      &lt;asp:TextBox ID="txtAddNatureOfWorkName" runat="server"  /&gt;
                  &lt;/FooterTemplate&gt;
              &lt;/asp:TemplateField&gt;
  
              &lt;asp:TemplateField HeaderText="Enable/Disable"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="lblNOWStatus" runat="server" Text='&lt;%# (int)Eval("IsNOWEnabled") == 1 ? "Enabled" : "Disabled" %&gt;' /&gt;
                  &lt;/ItemTemplate&gt;
                  &lt;EditItemTemplate&gt;
                      &lt;asp:RadioButtonList ID="radiolistStatus" runat="server"&gt;
                      &lt;asp:ListItem Text="Enable" Value="1"&gt;&lt;/asp:ListItem&gt;
                      &lt;asp:ListItem Text="Disable" Value="0"&gt;&lt;/asp:ListItem&gt;
                      &lt;/asp:RadioButtonList&gt;
                  &lt;/EditItemTemplate&gt;
                  &lt;FooterTemplate&gt;
                  &lt;asp:RadioButtonList ID="radiolistStatusAdd" runat="server"&gt;
                      &lt;asp:ListItem Text="Enable" Value="1"&gt;&lt;/asp:ListItem&gt;
                      &lt;asp:ListItem Text="Disable" Value="0"&gt;&lt;/asp:ListItem&gt;
                      &lt;/asp:RadioButtonList&gt;
                  &lt;/FooterTemplate&gt;
                  &lt;/asp:TemplateField&gt;
  
              &lt;asp:TemplateField  ShowHeader="False" &gt; 
                  &lt;EditItemTemplate&gt; 
                    &lt;asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"&gt;&lt;/asp:LinkButton&gt; 
                    &lt;asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"&gt;&lt;/asp:LinkButton&gt; 
                  &lt;/EditItemTemplate&gt; 
                  &lt;FooterTemplate&gt; 
                    &lt;asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="AddNew" Text="Add New"&gt;&lt;/asp:LinkButton&gt; 
                  &lt;/FooterTemplate&gt; 
                  &lt;ItemTemplate&gt; 
                    &lt;asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"&gt;&lt;/asp:LinkButton&gt; 
                  &lt;/ItemTemplate&gt; 
              &lt;/asp:TemplateField&gt; 
              &lt;asp:CommandField HeaderText="Delete" ShowDeleteButton="True"  ShowHeader="True" /&gt; 
              &lt;asp:CommandField ShowSelectButton="True" ItemStyle-CssClass="HiddenColumn" HeaderStyle-CssClass="HiddenColumn"/&gt; 
        &lt;/Columns&gt;
    &lt;/asp:GridView&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
  
  &lt;asp:GridView ID="GVRegionProject" runat="server" AllowPaging="true" ShowFooter="true" PageSize="10" AutoGenerateColumns="false"
       OnPageIndexChanging="GVRegionProject_PageIndexChanging" 
       onrowcancelingedit="GVRegionProject_RowCancelingEdit" 
       onrowcommand="GVRegionProject_RowCommand" onrowdeleting="GVRegionProject_RowDeleting" 
       onrowediting="GVRegionProject_RowEditing" onrowupdating="GVRegionProject_RowUpdating" 
       OnSelectedIndexChanged="GVRegionProject_SelectedIndexChanged"
       HeaderStyle-BackColor="BlueViolet" 
       HeaderStyle-ForeColor="White" BackColor="White"&gt;
       &lt;Columns&gt;
       &lt;asp:TemplateField HeaderText="Region/Project ID"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="lblRegionProjectID" runat="server" Text='&lt;%#Eval("RegionProjectID") %&gt;' /&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
  
              &lt;asp:TemplateField HeaderText="Region/Project Name"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="lblRegionProjectName" runat="server" Text='&lt;%#Eval("RegionProjectName") %&gt;' /&gt;
                  &lt;/ItemTemplate&gt;
                  &lt;EditItemTemplate&gt;
                      &lt;asp:TextBox ID="txtRegionProjectName" runat="server" Text='&lt;%#Eval("RegionProjectName") %&gt;' /&gt;
                  &lt;/EditItemTemplate&gt;
                  &lt;FooterTemplate&gt;
                      &lt;asp:TextBox ID="txtAddRegionProjectName" runat="server"  /&gt;
                  &lt;/FooterTemplate&gt;
              &lt;/asp:TemplateField&gt;
  
              &lt;asp:TemplateField HeaderText="Enable/Disable"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="lblRPStatus" runat="server" Text='&lt;%# (int)Eval("IsRegionProjectEnabled") == 1 ? "Enabled" : "Disabled" %&gt;' /&gt;
                  &lt;/ItemTemplate&gt;
                  &lt;EditItemTemplate&gt;
                      &lt;asp:RadioButtonList ID="radiolistStatus" runat="server"&gt;
                      &lt;asp:ListItem Text="Enable" Value="1"&gt;&lt;/asp:ListItem&gt;
                      &lt;asp:ListItem Text="Disable" Value="0"&gt;&lt;/asp:ListItem&gt;
                      &lt;/asp:RadioButtonList&gt;
                  &lt;/EditItemTemplate&gt;
                  &lt;FooterTemplate&gt;
                  &lt;asp:RadioButtonList ID="radiolistStatusAdd" runat="server"&gt;
                      &lt;asp:ListItem Text="Enable" Value="1"&gt;&lt;/asp:ListItem&gt;
                      &lt;asp:ListItem Text="Disable" Value="0"&gt;&lt;/asp:ListItem&gt;
                      &lt;/asp:RadioButtonList&gt;
                  &lt;/FooterTemplate&gt;
                  &lt;/asp:TemplateField&gt;
  
              &lt;asp:TemplateField  ShowHeader="False" &gt; 
                  &lt;EditItemTemplate&gt; 
                    &lt;asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"&gt;&lt;/asp:LinkButton&gt; 
                    &lt;asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"&gt;&lt;/asp:LinkButton&gt; 
                  &lt;/EditItemTemplate&gt; 
                  &lt;FooterTemplate&gt; 
                    &lt;asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="AddNew" Text="Add New"&gt;&lt;/asp:LinkButton&gt; 
                  &lt;/FooterTemplate&gt; 
                  &lt;ItemTemplate&gt; 
                    &lt;asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"&gt;&lt;/asp:LinkButton&gt; 
                  &lt;/ItemTemplate&gt; 
              &lt;/asp:TemplateField&gt; 
              &lt;asp:CommandField HeaderText="Delete" ShowDeleteButton="True"  ShowHeader="True" /&gt; 
              &lt;asp:CommandField HeaderText="County" ShowHeader="false" ShowSelectButton="True"/&gt;
  
  
   &lt;/Columns&gt;
   &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>Code Behind is :-</p>
  
  <pre><code>        private void BindGrid()
      {
          dataTable = new DataTable();
          cmd.Connection = conn;
          cmd.CommandText = "SELECT * FROM natureOfWork";
          ad = new SqlDataAdapter(cmd);
          ad.Fill(dataTable);
          GVNatureOFWork.DataSource = dataTable;
          GVNatureOFWork.DataBind();
      }
  
      protected void GVNatureOFWork_PageIndexChanging(object sender, GridViewPageEventArgs e)
      {
          GVNatureOFWork.PageIndex = e.NewPageIndex;
          BindGrid();
      }
  
      protected void GVNatureOFWork_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
      {
          GVNatureOFWork.EditIndex = -1;
          BindGrid();
  
      }
  
      protected void GVNatureOFWork_RowCommand(object sender, GridViewCommandEventArgs e)
      {
          if (e.CommandName.Equals("AddNew"))
          {
  
              TextBox txtAddNatureOfWorkName = (TextBox)GVNatureOFWork.FooterRow.FindControl("txtAddNatureOfWorkName");
              RadioButtonList radiolistStatusAdd = (RadioButtonList)GVNatureOFWork.FooterRow.FindControl("radiolistStatusAdd");
              if ((txtAddNatureOfWorkName.Text != "")&amp;&amp;(radiolistStatusAdd.SelectedValue != string.Empty))
               {
                  cmd.Connection = conn;
                  cmd.CommandText = "INSERT INTO NatureOFWork(NatureOFWorkname, isNOWEnabled ) Values('" + txtAddNatureOfWorkName.Text + "', '" + radiolistStatusAdd.SelectedValue.ToString() + "')";
                  conn.Open();
                  cmd.ExecuteNonQuery();
              }
              BindGrid();
              conn.Close();
          }
  
          if (e.CommandName.Equals("Select"))
          {
  
  
          }
      }
  
      protected void GVNatureOFWork_RowDeleting(object sender, GridViewDeleteEventArgs e)
      {
          Label lblNautureOfWorkID = (Label)GVNatureOFWork.Rows[e.RowIndex].FindControl("lblNautureOfWorkID");
          cmd.Connection = conn;
          cmd.CommandText = "DELETE FROM NatureOFWork WHERE NatureOfWorkID='" + lblNautureOfWorkID.Text.ToString() + "'";
          conn.Open();
          cmd.ExecuteNonQuery();
          conn.Close();
          BindGrid();
      }
  
      protected void GVNatureOFWork_RowEditing(object sender, GridViewEditEventArgs e)
      {
          GVNatureOFWork.EditIndex = e.NewEditIndex;            
          BindGrid();
  
      }
  
      protected void GVNatureOFWork_RowUpdating(object sender, GridViewUpdateEventArgs e)
      {
  
          Label lblNautureOfWorkID = (Label)GVNatureOFWork.Rows[e.RowIndex].FindControl("lblNautureOfWorkID");
          TextBox txtNatureOfWorkName = (TextBox)GVNatureOFWork.Rows[e.RowIndex].FindControl("txtNatureOfWorkName");
          RadioButtonList radiolistStatus = (RadioButtonList)GVNatureOFWork.Rows[e.RowIndex].FindControl("radiolistStatus");
          string isenabled = radiolistStatus.SelectedValue.ToString();
          cmd.Connection = conn;
          cmd.CommandText = "UPDATE NatureOfWork SET NatureOfWorkname ='" + txtNatureOfWorkName.Text + "',IsNowEnabled=" + isenabled.ToString() + "  WHERE NatureOFWorkID=" + lblNautureOfWorkID.Text.ToString();
          conn.Open();
          cmd.ExecuteNonQuery();
          GVNatureOFWork.EditIndex = -1;
          BindGrid();
          conn.Close();
      }
  
      protected void GVNatureOFWork_RowDataBound(object sender, GridViewRowEventArgs e)
      {
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              Label lblNautureOfWorkID = (Label)e.Row.FindControl("lblNautureOfWorkID");
              Label lblNatureOfWorkName = (Label)e.Row.FindControl("lblNatureOfWorkName");
              TextBox txtNatureOfWorkName = (TextBox)e.Row.FindControl("txtNatureOfWorkName");
              TextBox txtAddNatureOfWorkName = (TextBox)e.Row.FindControl("txtAddNatureOfWorkName");
              RadioButtonList radiolistStatusAdd = (RadioButtonList)e.Row.FindControl("radiolistStatusAdd");
              RadioButtonList radiolistStatus = (RadioButtonList)e.Row.FindControl("radiolistStatus");
              Label lblNOWStatus = (Label)e.Row.FindControl("lblNOWStatus");
  
  
          }
          if (e.Row.RowType == DataControlRowType.Footer)
          {
          }
      }
  
      protected void GVNatureOFWork_SelectedIndexChanged(object sender, EventArgs e)
      {
          string natureOfWorkID=GVNatureOFWork.SelectedRow.Cells[0].Text;
          BindRegionProjectInfoGrid();
      }
  </code></pre>