Groups | Blog | Home
all groups > asp.net > february 2008 >

asp.net : 'Gridview1' fired event Sorting which wasn't handled


Mark Rae [MVP]
2/23/2008 10:04:59 PM
[quoted text, click to view]

That's because you haven't wired up the paging and sorting events...

<asp:GridView runat="server" id="Gridview1"
AllowPaging="True" AllowSorting="true" pagesize="3"
OnPageIndexChanging="Gridview1_PageIndexChanging"
OnSorting="Gridview1_Sorting"
/>

protected void Gridview1_PageIndexChanging(object sender,
GridViewPageEventArgs e)
{
// do something
}

protected void Gridview1_Sorting(object sender, GridViewSortEventArgs e)
{
// do something
}


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Vincent
2/23/2008 10:50:31 PM
Hi,

I created a gridview bound to the roles tables with this code:
rolesArray = Roles.GetAllRoles()
Gridview1.DataSource = rolesArray
Gridview1.DataBind()

The gridview has following classic properties:
<asp:GridView runat="server" id="Gridview1" AllowPaging="True"
AllowSorting="true"
pagesize="3" />

When clicking on the bar containing the pagenumber of the gridview in order
to go the next page, i get:
"The GridView 'Gridview1' fired event PageIndexChanging which wasn't
handled."

When i click on the field for sorting, i get the error:
"The GridView 'Gridview1' fired event Sorting which wasn't handled"

Thanks for help
Vincent

Mark Rae [MVP]
2/24/2008 11:36:48 AM
[quoted text, click to view]

Yes, but you're not using the SqlDataSource directly i.e. you're not setting
the GridView's DataSourceID property - instead, you're using a custom object
as the GridView's datasource e.g.
rolesArray = Roles.GetAllRoles()
Gridview1.DataSource = rolesArray
Gridview1.DataBind()

That's why you need to add event handlers for the sorting and paging
functionality manually:
http://forums.asp.net/p/956540/1177923.aspx


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Vincent
2/24/2008 12:10:43 PM
Thanks for your reply.

Normally, when creating a gridview bound to a sqldatasource, the sorting /
paging occur automatically. This is new to me so what code do you mean with
// do something
Does it exist something like 'sort' or 'paging' ...?


Mark Rae [MVP]" <mark@markNOSPAMrae.net> schreef in bericht
news:%23NyphgmdIHA.4476@TK2MSFTNGP06.phx.gbl...
[quoted text, click to view]

Vincent
2/24/2008 6:23:07 PM
Thanks, i'll read it

"Mark Rae [MVP]" <mark@markNOSPAMrae.net> schreef in bericht
news:OwjVKmtdIHA.4684@TK2MSFTNGP06.phx.gbl...
[quoted text, click to view]

Mark Rae [MVP]
2/24/2008 9:12:19 PM
[quoted text, click to view]

Well, there would be... A DataTable is a DataTable datatype (obviously!),
but a GridView's DataSource is an object datatype so that various datatypes
can be used as datasources for databound controls - you can't dimension a
DataTable as an object directly... I can only imagine that you're not using
Option Strict, otherwise I'm pretty sure your code wouldn't have compiled -
it certainly wouldn't have compiled in C#...

[quoted text, click to view]

Try this:
Dim dt As DataTable = DirectCast(Gridview1.DataSource, DataTable)


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Vincent
2/24/2008 9:40:58 PM
Hi Mark,

i used the code you gave me but there is an error when sorting only:
"Unable to cast object of type 'System.String[]' to type
'System.Data.DataTable' "
at line: Dim dt As DataTable = Gridview1.DataSource

i tried several things (using string() instead of Datatable ...) but could
not find the solution.

the whole code:
Protected Sub Gridview1_Sorting(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewSortEventArgs) Handles Gridview1.Sorting
Dim dt As DataTable = Gridview1.DataSource
If Not IsDBNull(dt) Then
Dim dv As DataView = New DataView(dt)
dv.Sort = e.SortExpression
Gridview1.DataSource = dv
Gridview1.DataBind()
End If
End Sub




"Mark Rae [MVP]" <mark@markNOSPAMrae.net> schreef in bericht
news:OwjVKmtdIHA.4684@TK2MSFTNGP06.phx.gbl...
[quoted text, click to view]

Vincent
2/24/2008 11:13:47 PM
Thanks again, but i still get the same error at line:
Dim dt As DataTable = DirectCast(Gridview1.DataSource, DataTable)



"Mark Rae [MVP]" <mark@markNOSPAMrae.net> schreef in bericht
news:OKVNxnydIHA.748@TK2MSFTNGP04.phx.gbl...
[quoted text, click to view]

Mark Rae [MVP]
2/24/2008 11:28:17 PM
[quoted text, click to view]

[top-posting corrected]

[quoted text, click to view]

Hmm - OK... What datatype is GridView1.DataSource? Can it even be cast to a
DataTable type...?


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Vincent
2/25/2008 8:43:41 AM
The datasource are the roles from the table 'aspnet_roles' created when
creating membership users:
so the datasource is of type array string.

dim rolesArray() As String
rolesArray = Roles.GetAllRoles()
Gridview1.DataSource = rolesArray
Gridview1.DataBind()

Thanks


"Mark Rae [MVP]" <mark@markNOSPAMrae.net> schreef in bericht
news:eF1wvzzdIHA.4488@TK2MSFTNGP04.phx.gbl...
[quoted text, click to view]

Mark Rae [MVP]
2/25/2008 10:50:17 AM
[quoted text, click to view]

[top-posting corrected again]

[quoted text, click to view]

OK, so the datasource for the GridView *isn't* a DataTable - it's a string
array... That's fine, of course, except that you're trying to dimension a
DataTable variable and populate it with a string array - that's never going
to work without an explicit conversion...

In the link I suggested, the sorting method relies on the fact that the
datasource of the GridView is a DataTable, from which a DataView object can
be created. This isn't the case with string arrays, so you'll have to write
your own sorting routine - something like this:
http://www.thescripts.com/forum/thread384129.html


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Vincent
2/25/2008 2:42:30 PM
ok, thanks

"Mark Rae [MVP]" <mark@markNOSPAMrae.net> schreef in bericht
news:%235YU2w5dIHA.536@TK2MSFTNGP06.phx.gbl...
[quoted text, click to view]

AddThis Social Bookmark Button