title: using checkbox to select and delete row from gridiew datasource = session C# .net
body: <p>I have been searching on here and other sites for three days for a solution.  I need to select one or more rows (using checkbox) and delete selected row(s) from a gridview who's datasource is a session datatable as you will see.  Not interested in SQLConnections or LINQ Entities.  As a result I am not sure if I need to use e.commandarguments, or how to go about it, but I have tried them with no luck.</p>
  
  <p>Error is visible and clearly commented on CART.ASPX.CS page inside the following method</p>
  
  <p>if (chkRemCart != null &amp;&amp; chkRemCart.Checked)</p>
  
  <p>Any assistance would be greatly appreciated and if you require any further coding or explanations to help with a solution, just ask.
  Thank you in advance.</p>
  
  <p>CART.ASPX</p>
  
  <pre><code>&lt;asp:GridView ID="gvCart" runat="server" AutoGenerateColumns="False" ShowHeaderWhenEmpty="true" CellPadding="4"
          HeaderStyle-CssClass="header" EmptyDataText="You have successfully cleared your Shopping Cart"
          OnRowDataBound="gvCart_RowDataBound"  &gt;
          &lt;Columns&gt;
              &lt;asp:TemplateField HeaderText="Movie Selector"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:CheckBox ID="chkRemCart" runat="server" /&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
  
              &lt;asp:TemplateField HeaderText="Movie ID"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="lblMovieID" runat="server" Text='&lt;%# Eval("MovieId") %&gt;'&gt;&lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
  
              &lt;asp:TemplateField HeaderText="Name"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="lblName" runat="server" Text='&lt;%# Eval("Name") %&gt;'&gt;&lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
  
              &lt;asp:TemplateField HeaderText="Duration"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="lblDuration" runat="server" Text='&lt;%# Eval("Duration") %&gt;'&gt;&lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
  
              &lt;asp:TemplateField HeaderText="Genre"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="lblGenre" runat="server" Text='&lt;%# Eval("Genre") %&gt;'&gt;&lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
  
              &lt;asp:TemplateField HeaderText="Rating"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="lblRating" runat="server" Text='&lt;%# Eval("Rating") %&gt;'&gt;&lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
  
              &lt;asp:TemplateField HeaderText="Price"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="lblPrice" runat="server" Text='&lt;%# Eval("Price","{0:n}") %&gt;'&gt;&lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
  
              &lt;asp:TemplateField HeaderText="Description"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="lblDescription" runat="server" Text='&lt;%# Eval("Description") %&gt;'&gt;&lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
  
              &lt;asp:TemplateField HeaderText="Total"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="lblTotal" runat="server"&gt;&lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
          &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  
  &lt;asp:Button ID="btnRem" runat="server" OnClick="RemCart" Text="Remove Selection" CssClass="button" Title="Remove Movies From Cart" /&gt;
  </code></pre>
  
  <p>CART.ASPX.CS</p>
  
  <p>Page_Load calling RefreshPages() method.  gvCart displaying all columns and rows of items in Datasource/Session/Datatable</p>
  
  <pre><code>    public void RefreshPages()
      {
          if (Session["SelectedMovies"] != null)
          {
              DataTable MovieTable = (DataTable)Session["SelectedMovies"];
              gvCart.DataSource = MovieTable;
              gvCart.DataBind();
          }
      }
  
      protected void RemCart(object sender, EventArgs e)
      {
          // Check session exists 
          if (Session["selectedMovies"] != null)
          {
              // Opening / Retreiving DataTable.
              DataTable MovieTable = (DataTable)Session["SelectedMovies"];
  
              foreach (GridViewRow row in gvCart.Rows)
              {
                  CheckBox chkRemCart = row.Cells[0].FindControl("chkRemCart") as CheckBox;
  
                  if (chkRemCart != null &amp;&amp; chkRemCart.Checked)
                  {
                      //  Error appearing on line below.  Scroll right to read.
                      int MovieId = Convert.ToInt32(gvCart.DataKeys[row.RowIndex].Value); // Error here. //Additional information: Index was out of range. Must be non - negative and less than the size of the collection. 
  
                      MovieTable.Rows.RemoveAt(row.RowIndex);
  
                      //Saving session
                      Session["selectedMovies"] = MovieTable;
                      // Updating gvCart
                      gvCart.DataSource = MovieTable;
                      gvCart.DataBind();
                  }
              }
          }
      }
  </code></pre>