title: System.IndexOutOfRangeException: Cannot find column XYZ sorting GridView
body: <p>I need to add a <strong>sorting function</strong> to a column in a <strong>GridView</strong>. </p>
  
  <p>I've set the <strong>AllowSorting-property</strong> on the GridView to <strong>true</strong> and added <strong>sort expression</strong> to the column.</p>
  
  <pre><code>&lt;asp:BoundField DataField="doName" HeaderText="doName" SortExpression="doName" /&gt;
  </code></pre>
  
  <p>Unfortunately, the sorting isn't working in the GridView.</p>
  
  <p>Below is my <strong>code-behind-file</strong> for the column that should be able to sort, but I get the error:</p>
  
  <blockquote>
    <p>Exception Details: System.IndexOutOfRangeException: Cannot find column
    doName.</p>
  </blockquote>
  
  <p><strong>Code behind:</strong></p>
  
  <pre><code>string sortingDirection;
  DataSet dsProducts = new DataSet();
  DataTable dt1 = new DataTable();
  
  public SortDirection dir
  {
      get
      {
          if (ViewState["dirState"] == null)
          {
              ViewState["dirState"] = SortDirection.Ascending;
          }
          return (SortDirection)ViewState["dirState"];
      }
      set
      {
          ViewState["dirState"] = value;
      }
  }
  
  public string SortField
  {
      get
      {
          return (string)ViewState["SortField"] ?? "doName";
      }
      set
      {
          ViewState["SortField"] = value;
      }
  }
  
  protected void gvProducts_Sorting(object sender, GridViewSortEventArgs e)
  {
  
      sortingDirection = string.Empty;
      if (dir == SortDirection.Ascending)
      {
          dir = SortDirection.Descending;
          sortingDirection = "Desc";
      }
      else
      {
          dir = SortDirection.Ascending;
          sortingDirection = "Asc";
      }
  
  
      DataView sortedView = new DataView(dt1);
      sortedView.Sort = e.SortExpression + " " + sortingDirection;
      SortField = e.SortExpression;
      gvProducts.DataSource = sortedView;
      gvProducts.DataBind();
  }
  
  protected void gvProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {       
      if (dir == SortDirection.Ascending)
      {
          sortingDirection = "Asc";
      }
      else
      {
          sortingDirection = "Desc";
      }
  
      DataView sortedView = new DataView(dt1);
      sortedView.Sort = SortField + " " + sortingDirection;
      gvProducts.DataSource = sortedView;
      gvProducts.PageIndex = e.NewPageIndex;
      gvProducts.DataBind();
  }
  
  private void BindData()
  {
      gvProducts.DataSource = RetrieveProducts();
      gvProducts.DataBind();
  }
  
  private DataSet RetrieveProducts()
  {
      sql = "Select * from doTable;";
  
      dsProducts = new DataSet();
  
      using (OdbcConnection cn =
        new OdbcConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
      {
          cn.Open();
  
          using (OdbcCommand cmd = new OdbcCommand(sql, cn))
          {
  
              OdbcDataAdapter adapter = new OdbcDataAdapter(cmd);
              adapter.Fill(dsProducts);
  
              dt1 = new DataTable();
              dsProducts.Tables.Add(dt1);
          }
      }
  
      return dsProducts;
  }
  </code></pre>