title: Wanted to use auto sorting with GridView in ASP.Net with VB code
body: <p>I am using a Gridview to show data from one table, however usying my below code it's not allowing me to auto sort, I get an error stating "System.Web.HttpException: The GridView 'GridView1' fired event Sorting which wasn't handled" </p>
  
  <p>It should auto sort without the need for me to write code for sorting event, I have made it work but via the ASPX file, but I don't want to use that becuase I would like to change the query to do diffrent filters so I want to do this from the .vb file not .aspx file</p>
  
  <p>Here is my code</p>
  
  <pre><code>Imports System.Data.SqlClient
  
  
  
  Public Class WebForm1
     Inherits System.Web.UI.Page
  
     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  
        Dim cnnData As New SqlConnection(ConfigurationManager.ConnectionStrings("DataTestConnectionString").ConnectionString)
      'Dim adData As New SqlDataAdapter
      Dim dsData As New DataSet
      Dim strQry As String
  
      strQry = "SELECT Student_No, " &amp; _
                      "FName, " &amp; _
                      "MName, " &amp; _
                      "LName, " &amp; _
                      "Phone_No, " &amp; _
                      "Major, " &amp; _
                      "Start_Date, " &amp; _
                      "Status " &amp; _
               "FROM tblStudent"
  
  
  
      Dim adData As New SqlDataAdapter(strQry, cnnData)
  
      adData.Fill(dsData)
      GridView1.DataSource = dsData
  
      GridView1.DataBind()
  
  
  
      'cnnData.Close()
      'adData.Dispose()
  
  End Sub
  
  End Class
  </code></pre>
  
  <p>and here is my aspx markup:</p>
  
  <pre><code> &lt;asp:GridView 
           ID="GridView1" 
           runat="server" 
           AllowPaging="True" 
           AllowSorting="True" 
           AutoGenerateColumns="False" 
           DataKeyNames="Student_No" 
           Height="30%" 
           Width="90%"
           CssClass="GridViewCSS"
           PagerStyle-CssClass="GridViewPager" 
           AlternatingRowStyle-CssClass="GridViewAlt"
           DataGridViewLinkColumn="Student_No"
           &gt;
  
  
    &lt;AlternatingRowStyle CssClass="GridViewAlt"&gt;&lt;/AlternatingRowStyle&gt;
  
  
          &lt;Columns&gt;
  
  
          &lt;asp:hyperlinkfield 
               DataTextField ="Student_No"
               navigateurl="./studentedit.aspx"            
               headertext="Employee No"
               /&gt;
  
              &lt;asp:BoundField 
                   DataField="Student_No" 
                   HeaderText="Student No" 
                   ReadOnly="True" 
                   SortExpression="Student_No" 
                   FooterStyle-Font-Underline ="true"
                   /&gt;
  
  
              &lt;asp:BoundField 
                   DataField="FName" 
                   HeaderText="First Name" 
                   ReadOnly="True" 
                   SortExpression="FName" 
                   /&gt;
  
              &lt;asp:BoundField 
                   DataField="MName" 
                   HeaderText="Middle Initial" 
                   ReadOnly="True" 
                   SortExpression="MName" 
                   /&gt;
  
              &lt;asp:BoundField 
                   DataField="LName" 
                   HeaderText="Last Name" 
                   ReadOnly="True" 
                   SortExpression="LName" 
                   /&gt;
  
  
              &lt;asp:BoundField 
                   DataField="Phone_No" 
                   HeaderText="Phone Number" 
                   ReadOnly="True" 
                   SortExpression="Phone_No" 
                   /&gt;
  
              &lt;asp:BoundField 
                   DataField="Major" 
                   HeaderText="Major" 
                   ReadOnly="True" 
                   SortExpression="Major" 
                   /&gt;
  
              &lt;asp:BoundField 
                   DataField="start_date" 
                   HeaderText="Start Date" 
                   ReadOnly="True" 
                   SortExpression="Start_Date" 
                   /&gt;
  
              &lt;asp:BoundField 
                   DataField="Status" 
                   HeaderText="Status" 
                   ReadOnly="True" 
                   SortExpression="Status" 
                   /&gt;
  
          &lt;/Columns&gt;
  
   &lt;PagerStyle CssClass="GridViewPager"&gt;&lt;/PagerStyle&gt;
        &lt;/asp:GridView
  </code></pre>
  
  <p>Help is appreciated. </p>
  
  <p>Thanks!</p>