title: Programmatically sort GridView
body: <p>I am using a GridView in ASP.NET and am building it up programmatically. However, when I go to sort, I am getting an error thrown because the event is not being handled correctly. </p>
  
  <p>I've not worked with ASP.NET GridViews in a long time and am extremely rusty on this one. </p>
  
  <p>This is the code I have so far:</p>
  
  <pre><code>Public Sub GetData()
      Using sqlConn As New SqlConnection(_connstr)
          Dim sqlcmd As New SqlCommand()
          sqlcmd.Connection = sqlConn
          sqlcmd.CommandType = CommandType.StoredProcedure
          sqlcmd.CommandText = "dbo.uspGetEmailAudit"
          sqlcmd.Parameters.Add(requestIdParam)
          Using sqlda As New SqlDataAdapter(sqlcmd)
              sqlda.Fill(_dt)
          End Using
      End Using
  
      BindData(_dt)
  End Sub
  
  Private Sub BindData(dt As DataTable)
      GridView1.DataSource = _dt
      GridView1.AllowSorting = True
      GridView1.AllowPaging = True
      GridView1.PageSize = 15
      GridView1.DataBind()
  End Sub
  
  Protected Sub sorting(sender As Object, e As GridViewSortEventArgs)
      ViewState("sortexp") = e.SortExpression
      GridView1.DataSource = GetData()
      GridView1.DataBind()
  End Sub
  </code></pre>
  
  <p>The error that I am getting is:</p>
  
  <blockquote>
    <p>The GridView 'GridView1' fired event Sorting which wasn't handled.</p>
  </blockquote>