Selected Index Changed return null values on a populated Grid View
<p>I have a function that checks which sql query needs to be executed and return a DataTable, it is called searchData and is inside the CarsDataSet class</p>

<pre><code>DataTable dataTable = null; //before there is code to detect the sql command to execute
    if (sqlCommand != null)
    {
        SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand, con);
        dataTable = new DataTable();
        sqlDataAdapter.Fill(dataTable);
    }

    return dataTable;
</code></pre>

<p>I am using then, this function on a aspx.cs page in order to bind this DataTable to a GridView..</p>

<pre><code>GridViewCarsFiltered.DataSource = carsDataSet.searchData(type, manufacturer, maxPrice, minPrice, maxYear, minYear);
GridViewCarsFiltered.DataBind();
</code></pre>

<p>The GridView gets populated normally, but the problem comes when a try to get values of a row using an select index changed handler..</p>

<pre><code>protected void GridViewCarsFiltered_SelectedIndexChanged(object sender, EventArgs e)
{    
    string carId = GridViewCarsFiltered.SelectedRow.Cells[1].Text;
}
</code></pre>

<p>I am receiving an <strong>'ArgumentOutOfRangeException'</strong> saying that carId is null.</p>

<p>This is my GridView, the DataSourceID is null because I am setting that from code behind..</p>

<pre><code>&lt;asp:GridView ID="GridViewCarsFiltered" runat="server" CssClass="table table-striped table-hover" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" DataSourceID="" ForeColor="Black" OnSelectedIndexChanged="GridViewCarsFiltered_SelectedIndexChanged" DataKeyNames="Id"&gt;
    &lt;Columns&gt;
        &lt;asp:CommandField ShowSelectButton="True" /&gt;
        &lt;asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" /&gt;
        &lt;asp:BoundField DataField="type" HeaderText="type" SortExpression="type" /&gt;
        &lt;asp:BoundField DataField="manufacturer" HeaderText="manufacturer" SortExpression="manufacturer" /&gt;
        &lt;asp:BoundField DataField="model " HeaderText="model " SortExpression="model " /&gt;
        &lt;asp:BoundField DataField="colour" HeaderText="colour" SortExpression="colour" /&gt;
        &lt;asp:BoundField DataField="price" HeaderText="price" SortExpression="price" /&gt;
        &lt;asp:BoundField DataField="year" HeaderText="year" SortExpression="year" /&gt;
        &lt;asp:BoundField DataField="location" HeaderText="location" SortExpression="location" /&gt;
        &lt;asp:BoundField DataField="username" HeaderText="username" SortExpression="username" /&gt;
    &lt;/Columns&gt;
&lt;/asp:GridView&gt;
</code></pre>

<p>And this is a screenshot of the actual populated GridView</p>

<p><a href="http://i.stack.imgur.com/59mkV.png" rel="nofollow">enter image description here</a></p>

<p>I hope I've been clear enough and that some of you can help me...
Thank you in advance</p>