title: Binding GridView to SqlDataSource programmatically
body: <p>I'm trying to replicate the binding in my markup with something programmatic. 
  It seems I am forced to use a <strong>dataset</strong> to bind to rather than a SqlDataSource as shown in my markup. 
  Furthermore, when using a dataset, I suddenly lose paging, sorting, etc., and get errors that I need to handle them manually. 
  They were handled automatically before?? </p>
  
  <p><em>So here is my markup:</em></p>
  
  <pre><code>&lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True" 
      CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333" 
      GridLines="None" AllowPaging="True" AllowSorting="True" Width="520px"&gt;
      &lt;AlternatingRowStyle BackColor="White" ForeColor="#284775" /&gt;
      &lt;Columns&gt;
      &lt;/Columns&gt;
  &lt;/asp:GridView&gt;   
  &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" 
      ConnectionString="&lt;%$ ConnectionStrings:NORTHWNDConnectionString %&gt;" 
      SelectCommand="CustOrdersOrders" SelectCommandType="StoredProcedure"&gt;
      &lt;SelectParameters&gt;
          &lt;asp:Parameter Direction="ReturnValue" Name="RETURN_VALUE" Type="Int32" /&gt;
          &lt;asp:ControlParameter ControlID="txtCustId" DefaultValue="AROUT" 
              Name="CustomerID" PropertyName="Text" Type="String" /&gt;
      &lt;/SelectParameters&gt;
  &lt;/asp:SqlDataSource&gt;
  </code></pre>
  
  <p><em>I replaced it with something like this:</em></p>
  
  <pre><code>SqlDataAdapter adapter = new SqlDataAdapter();                
  conn.Open();
  using (SqlConnection conn = new SqlConnection(connStr))
  {
      SqlDataAdapter adapter = new SqlDataAdapter();
  
      conn.Open();
  
      using (SqlCommand cmd = new SqlCommand("CustOrdersOrders", conn))
      {
          cmd.CommandType = CommandType.StoredProcedure;
          cmd.Parameters.AddWithValue("CustomerID", "AROUT");
          cmd.ExecuteScalar();
          adapter.SelectCommand = cmd;
      }
  
      GridView1.DataSource = adapter;                
      GridView1.DataBind();
  }
  </code></pre>
  
  <p>This ofcourse raises an error that that the data source is not of the correct type, yet this is exactly what the markup does. 
  The only thing I can think of is that is uses <strong>DataSource</strong> instead of <strong>DataSourceID</strong>. 
  So how do I replicate this markup and get all the sorting and paging automatically? 
  If I have to use a dataset, why does the markup not require one? 
  But, even with the dataset, it doesn't seem to handle the paging and sorting automatically like I said.</p>
  
  <p>Thanks!
  Levi</p>