title: Why do I get a column not found error while sorting a GridView
body: <pre><code>&lt;asp:GridView AlternatingRowStyle-BackColor="#E2E2E2" AutoGenerateColumns="false" OnSorting="yourTasksGV_Sorting" AllowSorting="true" ID="yourTasksGV" runat="server" ClientIDMode="Static" EmptyDataText="There is no data to display" OnRowDataBound="yourTasksGV_RowDataBound"&gt;
      &lt;Columns&gt;
          &lt;asp:HyperLinkField Target="_blank" DataNavigateUrlFields="Task Detail" DataTextField="Task Name" DataNavigateUrlFormatString="" HeaderText="Task Details" SortExpression="TaskDetails" ItemStyle-Width="35%" ItemStyle-CssClass="taskTableColumn" /&gt;
          &lt;asp:BoundField DataField="Services" HeaderText="Services" SortExpression="ServiceName" ItemStyle-Width="25%" ItemStyle-CssClass="taskTableColumn" /&gt;
          &lt;asp:BoundField DataField="Status" HeaderText="Status" SortExpression="TheStatus" ItemStyle-Width="15%" ItemStyle-CssClass="taskTableColumn" /&gt;
          &lt;asp:BoundField DataField="Due Date" HeaderText="Due Date" SortExpression="DueDate" ItemStyle-Width="15%" ItemStyle-CssClass="taskTableColumn" /&gt;
      &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>code-behind:</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
  
      if (!Page.IsPostBack)
      {
          PullData();
      }
  }
  
  public void PullData()
  {
      DataTable taskData = new DataTable();
      string connString = @"user id = user;" + "password= pss$$$$; server= dev-magician; database= OnBase;" /*+ "Trusted_Connection=yes;"*/  + "connection timeout=30";
      string query = @"SELECT  'http://mg/appnet/workview/objectPop.aspx?objectid=' + CAST(CT.OBJECTID AS VARCHAR) + '&amp;classid=1224' 'Task Detail'
              ,UG.USERGROUPNAME 'Services'
              ,CT.ATTR2812 'Status'
              ,CT.ATTR2752 'Due Date'
              ,CT.ATTR2739 'Task Name'
  
          FROM HSI.RMOBJECTINSTANCE1224 CT LEFT JOIN HSI.USERGROUP UG on CT.FK2743 = UG.USERGROUPNUM
          WHERE CT.ACTIVESTATUS = 0";
  
      using (SqlConnection conn = new SqlConnection(connString))
      {
          try
          {
              SqlCommand cmd = new SqlCommand(query, conn);
  
              // create data adapter
              SqlDataAdapter da = new SqlDataAdapter(query, conn);
              // this will query your database and return the result to your datatable
              da.Fill(taskData);
              //conn.Close();
              yourTasksGV.DataSource = taskData;
              yourTasksGV.DataBind();
  
              Session["sortData"] = taskData;
  
          }
          catch (Exception ex)
          {
              string error = ex.Message;
          }
      }
  }
  
  protected void yourTasksGV_Sorting(object server, GridViewSortEventArgs e)
  {
      /*string strSortExpression = e.SortExpression;
      switch (strSortExpression)
      {
          case "TaskDetails":
              MessageBox.Show("Task Details");
              break;
      }*/
      DataTable dataTable = Session["sortData"] as DataTable;
  
      if (dataTable != null)
      {
          DataView dataView = new DataView(dataTable);
          dataView.Sort = e.SortExpression + " " + ConvertSortDirection(e.SortDirection);
  
          yourTasksGV.DataSource = dataView;
          yourTasksGV.DataBind();
      }
  }
  private string ConvertSortDirection(SortDirection sortDirection)
  {
      string newSortDirection = String.Empty;
  
      switch (sortDirection)
      {
          case SortDirection.Ascending:
              newSortDirection = "ASC";
              break;
  
          case SortDirection.Descending:
              newSortDirection = "DESC";
              break;
      }
  
      return newSortDirection;
  }
  </code></pre>
  
  <p>What I am looking to achieve is whenever I click on any of the header it should sort the GridView but no matter which header text I click on to sort, I keep getting the following error:</p>
  
  <pre><code>Exception Details: System.IndexOutOfRangeException: Cannot find column ServiceName.
  
  Source Error: 
  
  An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  
  
  Stack Trace: 
  
  
  [IndexOutOfRangeException: Cannot find column ServiceName.]
     System.Data.DataTable.ParseSortString(String sortString) +5313286
     System.Data.DataView.CheckSort(String sort) +35
     System.Data.DataView.set_Sort(String value) +115
     TestOnBase.Pages.Default.yourTasksGV_Sorting(Object server, GridViewSortEventArgs e) +256
     System.Web.UI.WebControls.GridView.OnSorting(GridViewSortEventArgs e) +122
     System.Web.UI.WebControls.GridView.HandleSort(String sortExpression, SortDirection sortDirection) +65
     System.Web.UI.WebControls.GridView.HandleSort(String sortExpression) +78
     System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +497
     System.Web.UI.WebControls.GridView.RaisePostBackEvent(String eventArgument) +205
     System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +13
     System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
     System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +9528578
     System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
  --------------------------------------------------------------------------------
  </code></pre>
  
  <p>How do I modify the code so that whichever header text the user clicks it, it sorts and then on another click it sorts the opposite and so forth?</p>
  
  <p>DataTable:</p>
  
  <p><img src="https://i.stack.imgur.com/uAWLV.png" alt="enter image description here"></p>
  
  <p>How it is displayed to the user:</p>
  
  <p><img src="https://i.stack.imgur.com/WcjRU.png" alt="enter image description here"></p>