title: Sorting Gridview When In Edit Not Working
body: <p>In my application I am sorting the gridview and also have Edit in that gridview, sorting works fine but when i click Edit, it puts the gridview in the original sorted order. when i click Edit - it lets me edit the row but it changes the order and show me the original record on that row in the edit mode.</p>
  
  <pre><code>private void LoadGridview()
      {
          DataTable dt = new DataTable();
  
          using (SqlConnection cs = new SqlConnection(connStr))
          {
              if (Session["sortedGV"] == null )
              {
                  DataSet ds = new DataSet();
                  SqlDataAdapter da = new SqlDataAdapter("spManagement_GetStationProfile", cs);
                  da.SelectCommand.CommandType = CommandType.StoredProcedure;
                  da.SelectCommand.Parameters.AddWithValue("@Que", ddlQue.SelectedValue);
                  da.Fill(ds);
                  dt = ds.Tables[0];
  
                  Session["sortedGV"] = new DataView(dt);
              }
          }
          gvStations.DataSource = Session["sortedGV"];
          gvStations.DataBind();
      }
  
  protected void gvStations_RowCommand(object sender, GridViewCommandEventArgs e)
  {
              if (e.CommandName == "EditRow")
              {
                  //This enables the EditTemplate
                  int rowindex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
                  gvStations.EditIndex = rowindex; //Enables the edit row in gridview                        
              }
              else if (e.CommandName == "CancelUpdate")
              {
                  gvStations.EditIndex = -1;
              }
              else if (e.CommandName == "UpdateRow")
              {
                  //DO SOME STUFF///
                  gvStations.EditIndex = -1; //Takes you out of update view      
              }
  
              if ((DataTable)Session["sortedGV"] != null)
              {                    
                  gvStations.DataSource = (DataTable)Session["sortedGV"];
              }
              else
              {
                  gvStations.DataSource = LoadGridview();
              }
              gvStations.DataBind();
              btnAddStation.Visible = true;
    }
    private void SortGridview(string sortExpression, string Direction)
      {
          DataView dv = null;
          DataTable dt = gvStations.DataSource as DataTable;
          dv = new DataView(dt);
  
          dv.Sort = sortExpression + " " + Direction;
          Session["sortedGV"] = dt;
          gvStations.DataSource = dv;
          gvStations.DataBind();
  
      }
  
      //Sorts gridview
      protected void gvStations_Sorting(object sender, GridViewSortEventArgs e)
      {
          String sortExpression = e.SortExpression;
          Session["SortExpression"] = sortExpression;
  
          if (Session["SortDirection"] != null &amp;&amp; Session["SortDirection"].ToString() == SortDirection.Descending.ToString())
          {
              Session["SortDirection"] = SortDirection.Ascending;
              SortGridview(sortExpression, "ASC");
          }
          else
          {
              Session["SortDirection"] = SortDirection.Descending;
              SortGridview(sortExpression, "DESC");
          }
  
      }
  </code></pre>
  
  <p>Gridview</p>
  
  <pre><code>&lt;asp:GridView ID="gvStations" runat="server" AutoGenerateColumns="False" DataKeyNames="StationID"
              Width="100%" AllowSorting="True" CellPadding="4" ForeColor="#333333" GridLines="None"
              OnRowCommand="gvStations_RowCommand" OnSorting="gvStations_Sorting"&gt;
              &lt;AlternatingRowStyle BackColor="White" ForeColor="#284775" /&gt;
              &lt;Columns&gt;
                  &lt;asp:TemplateField ShowHeader="False"&gt;
                      &lt;ItemTemplate&gt;
                          &lt;asp:LinkButton ID="lbEdit" runat="server" CausesValidation="False" CommandName="EditRow"
                              CommandArgument='&lt;%# Eval("StationId")%&gt;'&gt;Edit&lt;/asp:LinkButton&gt;
                      &lt;/ItemTemplate&gt;
                      &lt;EditItemTemplate&gt;
                          &lt;asp:LinkButton ID="lbUpdate" runat="server" CausesValidation="True" CommandName="UpdateRow"
                              ForeColor="White" Text="Update" CommandArgument='&lt;%# Eval("StationId")%&gt;'&gt;&lt;/asp:LinkButton&gt;
                          &lt;asp:LinkButton ID="lbCancel" runat="server" CausesValidation="False" CommandName="CancelUpdate"
                              ForeColor="White" CommandArgument='&lt;%# Eval("StationId")%&gt;' Text="Cancel"&gt;&lt;/asp:LinkButton&gt;
                      &lt;/EditItemTemplate&gt;
                      &lt;FooterStyle HorizontalAlign="Center" /&gt;
                      &lt;ItemStyle HorizontalAlign="Center" /&gt;
                  &lt;/asp:TemplateField&gt;
                  &lt;asp:BoundField DataField="StationID" HeaderText="ID" SortExpression="StationID"
                      Visible="false" ReadOnly="true"&gt;
                      &lt;ItemStyle HorizontalAlign="Center" /&gt;
                  &lt;/asp:BoundField&gt;
                  &lt;%--Station Number--%&gt;
                  &lt;asp:TemplateField HeaderText="#" SortExpression="StationNumber"&gt;
                      &lt;EditItemTemplate&gt;
                          &lt;asp:TextBox ID="tbStationNumberEdit" runat="server" Text='&lt;%# Eval("StationNumber") %&gt;' /&gt;
                      &lt;/EditItemTemplate&gt;
                      &lt;ItemTemplate&gt;
                          &lt;asp:Label ID="lbStationNumber" runat="server" Text='&lt;%# Eval("StationNumber") %&gt;' /&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
                &lt;/Columns&gt;  
            &lt;/asp:GridView&gt;
  </code></pre>