0
votes

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.

I've not worked with ASP.NET GridViews in a long time and am extremely rusty on this one.

This is the code I have so far:

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

The error that I am getting is:

The GridView 'GridView1' fired event Sorting which wasn't handled.

2
So how/where are you telling GridView1 to handle the Sorting event? - j.f.
Can we see your asp.net gridview. I just did this but c# stackoverflow.com/questions/33112464/… Here I show you how I sorted it and how my asp.net gridview looks. You may not be calling sort like j.f. says - KratosMafia

2 Answers

0
votes

Somewhere in Form_Load or otherwise, you need to wire up the OnSorting event.

Examples are here:

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sorting(v=vs.110).aspx

0
votes

In your GridView markup, tell it to use your sub:

<asp:GridView runat="server" OnSorting="sorting" id="GridView1" />

You have to wire it up, it won't do it for you.

If you create the GridView in code, you'll need to do the VB.NET equivalent of this C# code (sorry, I'm not a VB guy)

GridView1.OnSorting += sorted;