title: Gridview sorting not working in updatepanel
body: <pre><code>&lt;div style="width: 90%; margin-right: 5%; margin-left: 5%; text-align: center"&gt;
      &lt;asp:UpdatePanel ID="UpdatePanel1"
                       runat="server"&gt;
          &lt;ContentTemplate&gt;
              &lt;asp:GridView ID="GridView7"
                            runat="server"
                            AutoGenerateColumns="False"
                            DataKeyNames="DetailsId"
                            GridLines="Horizontal"
                            OnRowCommand="GridView7Data_RowCommand"
                            EnableSortingAndPagingCallbacks="false"
                            onpageindexchanging="GridView7_PageIndexChanging"
                            AllowPaging="True"
                            AllowSorting="True"
                            onsorting="GridView7_Sorting"&gt;
                  &lt;Columns&gt;
                      &lt;asp:ButtonField CommandName="editRecord"
                                       ControlStyle-CssClass="btn"
                                       ButtonType="Button"
                                       Text="Edit"
                                       HeaderText="Edit Record"&gt;
                          &lt;ControlStyle CssClass="btn"&gt;&lt;/ControlStyle&gt;
                      &lt;/asp:ButtonField&gt;
                      &lt;asp:BoundField DataField="Name"
                                      HeaderText="Name"
                                      SortExpression="Name" /&gt;
                      &lt;asp:BoundField DataField="AdminNo"
                                      HeaderText="Admin No"
                                      SortExpression="AdminNo" /&gt;
                      &lt;asp:BoundField DataField="FName"
                                      HeaderText="File Name" /&gt;
                      &lt;asp:TemplateField HeaderText="Download Link"
                                         ItemStyle-HorizontalAlign="Center"&gt;
                          &lt;ItemTemplate&gt;
                              &lt;asp:LinkButton ID="lnkDownload7"
                                              runat="server"
                                              CommandArgument='&lt;%# Eval("DetailsId") %&gt;'
                                              OnClick="DownloadFile7"
                                              Text="Download"&gt;&lt;/asp:LinkButton&gt;
                          &lt;/ItemTemplate&gt;
                          &lt;ItemStyle HorizontalAlign="Center" /&gt;
                      &lt;/asp:TemplateField&gt;
                      &lt;asp:TemplateField HeaderText="Image1"
                                         ItemStyle-Height="150"
                                         ItemStyle-Width="170"&gt;
                          &lt;EditItemTemplate&gt;
                              &lt;asp:TextBox ID="TextBox24"
                                           runat="server"
                                           Text='&lt;%# Eval("image") %&gt;'&gt;&lt;/asp:TextBox&gt;
                          &lt;/EditItemTemplate&gt;
                          &lt;ItemTemplate&gt;
                              &lt;asp:Image ID="Image19"
                                         runat="server"
                                         ImageUrl='&lt;%# Eval("image", GetUrl("{0}")) %&gt;' /&gt;
                          &lt;/ItemTemplate&gt;
                          &lt;ControlStyle Height="170px"
                                        Width="150px" /&gt;
                          &lt;ItemStyle Height="170px"
                                     Width="150px" /&gt;
                      &lt;/asp:TemplateField&gt;
                      &lt;asp:TemplateField HeaderText="Image2"&gt;
                          &lt;EditItemTemplate&gt;
                              &lt;asp:TextBox ID="TextBox25"
                                           runat="server"
                                           Text='&lt;%# Eval("image2") %&gt;'&gt;&lt;/asp:TextBox&gt;
                          &lt;/EditItemTemplate&gt;
                          &lt;ItemTemplate&gt;
                              &lt;asp:Image ID="Image20"
                                         runat="server"
                                         ImageUrl='&lt;%# Eval("image2", GetUrl("{0}")) %&gt;' /&gt;
                          &lt;/ItemTemplate&gt;
                          &lt;ControlStyle Height="170px"
                                        Width="150px" /&gt;
                          &lt;ItemStyle Height="170px"
                                     Width="150px" /&gt;
                      &lt;/asp:TemplateField&gt;
                      &lt;asp:TemplateField HeaderText="Image3"&gt;
                          &lt;EditItemTemplate&gt;
                              &lt;asp:TextBox ID="TextBox26"
                                           runat="server"
                                           Text='&lt;%# Eval("image3") %&gt;'&gt;&lt;/asp:TextBox&gt;
                          &lt;/EditItemTemplate&gt;
                          &lt;ItemTemplate&gt;
                              &lt;asp:Image ID="Image21"
                                         runat="server"
                                         ImageUrl='&lt;%# Eval("image3", GetUrl("{0}")) %&gt;' /&gt;
                          &lt;/ItemTemplate&gt;
                          &lt;ControlStyle Height="170px"
                                        Width="150px" /&gt;
                          &lt;ItemStyle Height="170px"
                                     Width="150px" /&gt;
                      &lt;/asp:TemplateField&gt;
                      &lt;asp:TemplateField HeaderText="Image4"
                                         SortExpression="image4"&gt;
                          &lt;EditItemTemplate&gt;
                              &lt;asp:TextBox ID="TextBox1"
                                           runat="server"
                                           Text='&lt;%# Eval("image4") %&gt;'&gt;&lt;/asp:TextBox&gt;
                          &lt;/EditItemTemplate&gt;
                          &lt;ItemTemplate&gt;
                              &lt;asp:Image ID="Image3"
                                         runat="server"
                                         ImageUrl='&lt;%# Eval("image4", GetUrl("{0}")) %&gt;' /&gt;
                          &lt;/ItemTemplate&gt;
                          &lt;ControlStyle Height="170px"
                                        Width="150px" /&gt;
                          &lt;ItemStyle Height="170px"
                                     Width="150px" /&gt;
                      &lt;/asp:TemplateField&gt;
                      &lt;asp:TemplateField HeaderText="Image5"
                                         SortExpression="image5"&gt;
                          &lt;EditItemTemplate&gt;
                              &lt;asp:TextBox ID="TextBox3"
                                           runat="server"
                                           Text='&lt;%# Eval("image5") %&gt;'&gt;&lt;/asp:TextBox&gt;
                          &lt;/EditItemTemplate&gt;
                          &lt;ItemTemplate&gt;
                              &lt;asp:Image ID="Image4"
                                         runat="server"
                                         ImageUrl='&lt;%# Eval("image5", GetUrl("{0}")) %&gt;' /&gt;
                          &lt;/ItemTemplate&gt;
                          &lt;ControlStyle Height="170px"
                                        Width="150px" /&gt;
                          &lt;ItemStyle Height="170px"
                                     Width="150px" /&gt;
                      &lt;/asp:TemplateField&gt;
                  &lt;/Columns&gt;
                  &lt;PagerStyle BackColor="White"
                              ForeColor="Black"
                              HorizontalAlign="Right" /&gt;
                  &lt;EmptyDataTemplate&gt;
                      &lt;asp:Label ID="Label29"
                                 runat="server"
                                 ForeColor="Red"
                                 Text="No Record Found"&gt;&lt;/asp:Label&gt;
                  &lt;/EmptyDataTemplate&gt;
                  &lt;SelectedRowStyle BackColor="#FFFFCC"
                                    Font-Bold="True"
                                    ForeColor="Black" /&gt;
                  &lt;HeaderStyle BackColor="#333333"
                               Font-Bold="True"
                               ForeColor="White" /&gt;
                  &lt;AlternatingRowStyle BackColor="White"
                                       ForeColor="#000000" /&gt;
              &lt;/asp:GridView&gt;
          &lt;/ContentTemplate&gt;
      &lt;/asp:UpdatePanel&gt;
  </code></pre>
  
  <p>The above is my code for my Gridview table. And currently my issues is that my sorting isn't working properly. However when it is placed outside of the Updatepanel, it is working. </p>
  
  <pre><code>protected void GridView7_Sorting(object sender, GridViewSortEventArgs e)
      {
          DataTable dtResult = (DataTable)ViewState["dt"];
          if (dtResult.Rows.Count &gt; 0)
          {
              if (Convert.ToString(ViewState["Sort"]) == "Asc")
              {
                  dtResult.DefaultView.Sort = e.SortExpression + " Desc";
                  //ViewState["sort"] == "Desc";
              }
              else
              {
                  dtResult.DefaultView.Sort = e.SortExpression + " Asc";
                  //ViewState["sort"] == "Asc";
              }
              GridView7.DataSource = dtResult;
              GridView7.DataBind();
          }
      }
  </code></pre>
  
  <p>This is my code behind for sorting. Any help and suggestions would be greatly appreciated. Thank you!</p>
  
  <p>This is the error</p>
  
  <pre><code>System.FormatException was unhandled by user code
  HResult=-2146233033
  Message=Input string was not in a correct format.
  Source=mscorlib
  StackTrace:
     at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer&amp; number, NumberFormatInfo info, Boolean parseDecimal)
     at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
     at System.String.System.IConvertible.ToInt32(IFormatProvider provider)
     at System.Convert.ToInt32(Object value)
     at StarsWebSite.PopGraduateTracking.GridView7Data_RowCommand(Object sender, GridViewCommandEventArgs e) in C:\Users\133214J\Desktop\latesttt\StarsWebSite\StarsWebSite\PopGenGraduateTracking.aspx.cs:line 109
     at System.Web.UI.WebControls.GridView.OnRowCommand(GridViewCommandEventArgs e)
     at System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup)
     at System.Web.UI.WebControls.GridView.RaisePostBackEvent(String eventArgument)
     at System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
     at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
     at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
     at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: 
  </code></pre>
  
  <p>This is my rowCommand method</p>
  
  <pre><code>protected void GridView7Data_RowCommand(object sender, GridViewCommandEventArgs e)
      {
          int index = Convert.ToInt32(e.CommandArgument);
          if (e.CommandName.Equals("editRecord"))
          {
              lblID.Text = GridView7.DataKeys[index].Value.ToString();
              DataTable dt1 = new DataTable();
              dt1 = GetData("select FamilyName,Name,Handphone,EmailAddress,Talent,AdminNo,DiplomaName,PMG,PEM,GPA,OSEP,EntryMode,Scholarship,NameOfScholarship,Award1,Quote1,NameOfDegree,UniversityAdmitted,YearAdmitted,SelfEmployed,LatestEmploymentChecked,LatestEmployer,CurrentPosition,CurrentSalary,Remarks, submittedBy from GraduateTracks where DetailsId = '" + int.Parse(lblID.Text) + "'");
  
              if (dt1.Rows.Count &gt; 0)
              {
                  // DataRow dr = dt.Select("CustomerID=" + GridViewData.DataKeys[gvrow.RowIndex].Value.ToString())[0];
                  TBFamilyName.Text = dt1.Rows[0]["FamilyName"].ToString(); //
                  TBName.Text = dt1.Rows[0]["Name"].ToString(); ;
                  TBHandphone.Text = dt1.Rows[0]["Handphone"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[3].Text);
                  TBEmailAddress.Text = dt1.Rows[0]["EmailAddress"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[4].Text);
                  TBTalent.Text = dt1.Rows[0]["Talent"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[5].Text);
                  lblAdminNo.Text = dt1.Rows[0]["AdminNo"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[6].Text);
                  TBDiplomaName.Text = dt1.Rows[0]["DiplomaName"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[7].Text);
                  TBPMG.Text = dt1.Rows[0]["PMG"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[8].Text);
                  TBPEM.Text = dt1.Rows[0]["PEM"].ToString();//HttpUtility.HtmlDecode(gvrow.Cells[9].Text);
                  TBGPA.Text = dt1.Rows[0]["GPA"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[10].Text);
                  TBOSEP.Text = dt1.Rows[0]["OSEP"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[11].Text);
                  TBEntryMode.Text = dt1.Rows[0]["EntryMode"].ToString(); //
                  TBScholarship.Text = dt1.Rows[0]["Scholarship"].ToString(); ;
                  TBNameOfScholarship.Text = dt1.Rows[0]["NameOfScholarship"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[3].Text);
                  TBAward1.Text = dt1.Rows[0]["Award1"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[4].Text);
                  TBQuote1.Text = dt1.Rows[0]["Quote1"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[5].Text);
                  TBNameOfDegree.Text = dt1.Rows[0]["NameOfDegree"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[6].Text);
                  TBUniversityAdmitted.Text = dt1.Rows[0]["UniversityAdmitted"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[7].Text);
                  TBYearAdmitted.Text = dt1.Rows[0]["YearAdmitted"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[8].Text);
                  TBSelfEmployed.Text = dt1.Rows[0]["SelfEmployed"].ToString();//HttpUtility.HtmlDecode(gvrow.Cells[9].Text);
                  TBLatestEmploymentChecked.Text = dt1.Rows[0]["LatestEmploymentChecked"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[10].Text);
                  TBLatestEmployer.Text = dt1.Rows[0]["LatestEmployer"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[11].Text);
                  TBCurrentPosition.Text = dt1.Rows[0]["CurrentPosition"].ToString(); //
                  TBCurrentSalary.Text = dt1.Rows[0]["CurrentSalary"].ToString(); ;
                  TBRemarks.Text = dt1.Rows[0]["Remarks"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[3].Text);
                  lblSubmit.Text = dt1.Rows[0]["submittedBy"].ToString(); //HttpUtility.HtmlDecode(gvrow.Cells[10].Text);
              }
              System.Text.StringBuilder sb = new System.Text.StringBuilder();
              sb.Append(@"&lt;script type='text/javascript'&gt;");
              sb.Append("$('#editModal').modal('show');");
              sb.Append(@"&lt;/script&gt;");
              ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "EditModalScript", sb.ToString(), false);
  
          }
      }
  </code></pre>