title: "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" in ASP.net
body: <p>I want to display the "NDA ID" in a label When the user click on the select link in the grid view. But it gives the error as "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index". </p>
  
  <p>Here is my code,</p>
  
  <pre><code>&lt;asp:GridView ID="grdNDA" runat="server" ShowHeaderWhenEmpty="true" EmptyDataText="No Records Found" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateSelectButton="true" Font-Size="Small" Width="95%" onselectedindexchanged="grdNDA_SelectedIndexChanged" &gt;
      &lt;AlternatingRowStyle BackColor="White" /&gt;
  
      &lt;Columns&gt;
          &lt;asp:BoundField HeaderText="NDA ID" DataField="NDAid" DataFormatString="ISM/C/NDA/IT/{0}" /&gt;
          &lt;asp:BoundField HeaderText="Company Name" DataField="ComName" /&gt;
          &lt;asp:BoundField HeaderText="Country Incorperated" DataField="Country" /&gt;
          &lt;asp:BoundField HeaderText="Date Created" DataField="Date" /&gt;
       &lt;/Columns&gt;
  
       &lt;EditRowStyle BackColor="#2461BF" /&gt;
       &lt;FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /&gt;
       &lt;HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /&gt;
       &lt;PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /&gt;
       &lt;RowStyle BackColor="#EFF3FB" /&gt;
       &lt;SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /&gt;
       &lt;SortedAscendingCellStyle BackColor="#F5F7FB" /&gt;
       &lt;SortedAscendingHeaderStyle BackColor="#6D95E1" /&gt;
       &lt;SortedDescendingCellStyle BackColor="#E9EBEF" /&gt;
       &lt;SortedDescendingHeaderStyle BackColor="#4870BE" /&gt;
  &lt;/asp:GridView&gt; 
  
  &lt;br&gt;
  &lt;asp:Label ID="lblValues" runat="server" Text=""&gt;&lt;/asp:Label&gt; 
  </code></pre>
  
  <p>Code behind</p>
  
  <pre><code>protected void grdNDA_SelectedIndexChanged(object sender, EventArgs e)
  {        
     lblValues.Text = "&lt;b&gt;ID:&lt;/b&gt; " + grdNDA.SelectedRow.Cells[2].Text;
  }
  </code></pre>