title: Get Item from GridViewRow ASP.NET
body: <p>I have a GridView which I Bind a DataSource to it from a SQL Database, the grid has a column with a checkbox in it so I can "select" a few Item in it (Rows actually). What I want here is to do some updates on the Item in each selected rows, but after some search I can't find how to access the Item in a Row, I though DataItem would work, but it's giving me a Null.</p>
  
  <p><strong>Edit:</strong> To make it short, I have a GridView which is built from a DataSource, so basically, each rows represent an Object, when I have a checkbox checked in one of the rows, I want to be able to grab the Object related to that Row, what is the easiest way to achieve that?</p>
  
  <p>The DataBinding on Page_Load:</p>
  
  <blockquote>
  <pre><code>  if (!Page.IsPostBack)
      {
          gvUnusedAccessories.DataSource = currentContext.Items.OfType&lt;Accessory&gt;().Where(ac =&gt; ac.Item_Parent_Id == ((PhoneLine)currentItem).Parent.Item_Id);
          gvUnusedAccessories.AutoGenerateColumns = false;
          gvUnusedAccessories.DataBind();
      }
  </code></pre>
  </blockquote>
  
  <p>The event when I press the Update Button, It actually browse the rows and if the row has a checked box it's gonna do the update:</p>
  
  <blockquote>
  <pre><code>protected void btnAddToMobile_Click(object sender, EventArgs e)
  {
      foreach (GridViewRow row in gvUnusedAccessories.Rows)
      {
          if(((CheckBox)row.FindControl("chkSelect")).Checked)
          {
              ((Accessory)row.DataItem).PhoneLine_Id = currentItem.Item_Id;
          }
      }
  }
  </code></pre>
  </blockquote>
  
  <p>And here's my GridView in the .aspx :</p>
  
  <pre><code> &lt;asp:GridView ID="gvUnusedAccessories" runat="server"&gt;
      &lt;Columns&gt;
          &lt;asp:CommandField ShowSelectButton="True" /&gt;
          &lt;asp:TemplateField HeaderText="Select"&gt;
              &lt;ItemTemplate &gt;
                  &lt;asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="True"/&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:BoundField DataField="Item_Id" HeaderText="ID" ReadOnly="True"/&gt;
          &lt;asp:BoundField DataField="Item_Name" HeaderText="Name" ReadOnly="True"/&gt;  
          &lt;asp:BoundField DataField="AccessoryModel" HeaderText="Modèle" ReadOnly="True"/&gt;
          &lt;asp:BoundField DataField="AccessoryBrand" HeaderText="Marque" ReadOnly="True"/&gt;
      &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>Any hint on how to access the Object contained inside a Row?
  I know I could actually get the ID and then do a SQL request to my DB, but it seems to be a bit heavy and there must be a better solution.</p>