Dynamically created Gridview fired event Sorting which wasn't handled
<p>I have a GridView which is getting it's columns dynamically added from the codebehind.  I'm not sure how to add sorting to it.  I've seen some tips online but I don't really get how to incorporate them because of how my columns are being added in codebehind.  Also I'd like this GridView to eventually create the columns dynamically based on user selection, so I wouldn't know ahead of time what columns are being added, and it would be fully dynamic.</p>

<p>Can anyone help direct me how to add sorting to what I already have?</p>

<p>Here's the GridView code:</p>

<pre><code>&lt;asp:GridView id="GridView3" runat="server" AutoGenerateColumns="False"
EmptyDataText="There are no data records to display."
     AllowPaging="True"  AllowSorting="True"
     OnPageIndexChanging="GridView3_PageIndexChanging"

    CssClass="GridViewStyle" GridLines="None" Width="100%"&gt;
&lt;Columns&gt;


&lt;asp:HyperLinkField DataNavigateUrlFields="EmplID"
    DataNavigateUrlFormatString="EmployeeProfile.aspx?EmplID={0}"
    DataTextField="EmplID"
    DataTextFormatString= "&lt;img src='Images/icons/document-search-result.png' alt='View'/&gt; &lt;u&gt;View&lt;/u&gt;" &gt;
    &lt;ControlStyle CssClass="titleLinksB" /&gt;
&lt;ItemStyle Wrap="False" /&gt;
&lt;/asp:HyperLinkField&gt;

&lt;/Columns&gt;
&lt;RowStyle CssClass="RowStyle" /&gt;
    &lt;EmptyDataRowStyle CssClass="EmptyRowStyle" /&gt;
    &lt;PagerSettings Mode="NumericFirstLast" PageButtonCount="5" /&gt;
    &lt;PagerStyle CssClass="PagerStyle" /&gt;
    &lt;SelectedRowStyle CssClass="SelectedRowStyle" /&gt;
    &lt;HeaderStyle CssClass="HeaderStyle" /&gt;
    &lt;EditRowStyle CssClass="EditRowStyle" /&gt;
    &lt;AlternatingRowStyle CssClass="AltRowStyle" /&gt;
    &lt;SortedAscendingHeaderStyle CssClass="sortasc"&gt;&lt;/SortedAscendingHeaderStyle&gt;
    &lt;SortedDescendingHeaderStyle CssClass="sortdesc"&gt;&lt;/SortedDescendingHeaderStyle&gt;
&lt;/asp:GridView&gt;
</code></pre>

<p>Here's the full page codebehind:</p>

<pre><code>Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


    If Not IsPostBack Then

        Dim curLastName As New BoundField
        curLastName.HeaderText = "Last Name"
        curLastName.DataField = "LastName"
        curLastName.SortExpression = "LastName"
        GridView3.Columns.Insert(0, curLastName)

        Dim curFirstName As New BoundField
        curFirstName.HeaderText = "First Name"
        curFirstName.DataField = "FirstName"
        curFirstName.SortExpression = "FirstName"
        GridView3.Columns.Insert(1, curFirstName)

        loadDynamicGrid()
    End If

End Sub


Private Sub loadDynamicGrid()

    Dim connetionString As String
    Dim connection As SqlConnection
    Dim command As SqlCommand
    Dim adapter As New SqlDataAdapter
    Dim ds As New DataSet
    Dim sql As String

    Dim lastName As String

    lastName = Request.QueryString("lastName")

    connetionString = ConfigurationManager.ConnectionStrings("dbConnectionString").ConnectionString.ToString()
    sql = "SELECT * FROM [EmployeeList] Where [lastname] like '" &amp; lastName &amp; "%' order by lastname"

    connection = New SqlConnection(connetionString)

    Try
        connection.Open()
        command = New SqlCommand(sql, connection)
        adapter.SelectCommand = command
        adapter.Fill(ds)

        GridView3.Visible = True
        GridView3.DataSource = ds
        GridView3.DataBind()

        adapter.Dispose()
        command.Dispose()
        connection.Close()

    Catch ex As Exception
        MsgBox("Can not open connection ! ")
    End Try

End Sub

Protected Sub GridView3_PageIndexChanging(ByVal sender As [Object], ByVal e As GridViewPageEventArgs)
    GridView3.PageIndex = e.NewPageIndex
    loadDynamicGrid()
End Sub
</code></pre>

<p>Any help is greatly appreciated!</p>
