From: Jerry on
If you are setting the DataSource of your GridView programmatically, set its DataSourceID property instead of DataSource; suddenly you'll be able to use the default sorting function.



Vincent wrote:

'Gridview1' fired event Sorting which wasn't handled
23-Feb-08

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

Previous Posts In This Thread:

On Saturday, February 23, 2008 4:50 PM
Vincent wrote:

'Gridview1' fired event Sorting which wasn't handled
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

On Saturday, February 23, 2008 5:04 PM
Mark Rae [MVP] wrote:

Re: 'Gridview1' fired event Sorting which wasn't handled
"Vincent" <vi,@sd.cv> wrote in message
news:%23XMLcYmdIHA.3940(a)TK2MSFTNGP05.phx.gbl...


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

On Sunday, February 24, 2008 6:10 AM
Vincent wrote:

Thanks for your reply.
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(a)markNOSPAMrae.net> schreef in bericht
news:%23NyphgmdIHA.4476(a)TK2MSFTNGP06.phx.gbl...

On Sunday, February 24, 2008 6:36 AM
Mark Rae [MVP] wrote:

Re: 'Gridview1' fired event Sorting which wasn't handled
"Vincent" <vi,@sd.cv> wrote in message
news:embJoXtdIHA.4396(a)TK2MSFTNGP02.phx.gbl...


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

On Sunday, February 24, 2008 12:23 PM
Vincent wrote:

Re: 'Gridview1' fired event Sorting which wasn't handled
Thanks, i'll read it

"Mark Rae [MVP]" <mark(a)markNOSPAMrae.net> schreef in bericht

On Sunday, February 24, 2008 3:40 PM
Vincent wrote:

Hi Mark,i used the code you gave me but there is an error when sorting
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(a)markNOSPAMrae.net> schreef in bericht
news:OwjVKmtdIHA.4684(a)TK2MSFTNGP06.phx.gbl...

On Sunday, February 24, 2008 4:12 PM
Mark Rae [MVP] wrote:

Re: 'Gridview1' fired event Sorting which wasn't handled
"Vincent" <vi,@sd.cv> wrote in message
news:eFuzOWydIHA.4728(a)TK2MSFTNGP03.phx.gbl...


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#...


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


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

On Sunday, February 24, 2008 5:13 PM
Vincent wrote:

Re: 'Gridview1' fired event Sorting which wasn't handled
Thanks again, but i still get the same error at line:
Dim dt As DataTable = DirectCast(Gridview1.DataSource, DataTable)



"Mark Rae [MVP]" <mark(a)markNOSPAMrae.net> schreef in bericht

On Sunday, February 24, 2008 6:28 PM
Mark Rae [MVP] wrote:

Re: 'Gridview1' fired event Sorting which wasn't handled
[top-posting corrected]


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

On Monday, February 25, 2008 2:43 AM
Vincent wrote:

The datasource are the roles from the table 'aspnet_roles' created when
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(a)markNOSPAMrae.net> schreef in bericht
news:eF1wvzzdIHA.4488(a)TK2MSFTNGP04.phx.gbl...

On Monday, February 25, 2008 5:50 AM
Mark Rae [MVP] wrote:

Re: 'Gridview1' fired event Sorting which wasn't handled
"Vincent" <vi,@sd.cv> wrote in message
news:%2302siI4dIHA.5900(a)TK2MSFTNGP02.phx.gbl...

[top-posting corrected again]


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

On Monday, February 25, 2008 8:42 AM
Vincent wrote:

Re: 'Gridview1' fired event Sorting which wasn't handled
ok, thanks

"Mark Rae [MVP]" <mark(a)markNOSPAMrae.net> schreef in bericht

On Thursday, March 26, 2009 7:09 AM
Jaimee Kaiser wrote:

'Gridview1' fired event Sorting which wasn't handled
Hi Mark..

I have a similar problem. My array is a type of an object and not a string array or anything like that. My array looks like this in the gridview:

Nr Contact Nr Customer Nr Date .... and so on
-- ---------- ----------- ---- ....
s1 K123 123 31.01.08
s2 K123 123 30.01.08
s3 K123 123 25.01.08

I want to sort by Nr (allowing the user to click on the header link for sorting).

I have tried numerous solutions to try and sort this gridview and I am absolutely at a wall.

My datasource looks like this:

servOrders = new GetServiceOrders();

ServiceHeader.ServHead[] tempOrders = new ServiceHeader.ServHead[100];

tempOrders = servOrders.GetServHeadByCustNo((string)Session["CustomerNo"], "Pending|In Process");

GridView2.DataSource = tempOrders;
GridView2.Databind();

I must use this way for datasource because it comes out of a deeper web service that gets the data live from Microsoft Navision (ie... the fields can change at anytime and the fields must be dynamic).

The temp order that is returned has 26 dimensions
(for example say 3 rows returned and in each row there is x amount of dimensions (field property and field value):
{0} --
Nr - Nr
Nr.value - S1
Contact_Nr - Contact Nr
Contact_Nr.value - K123
... and so one
{1} --
Nr - Nr
Nr.value - S2
Contact_Nr - Contact Nr
Contact_Nr.value - K123
... and so one

How on earth do I sort this?? I can't put it into a datatable.. I tried that and the datatable kept returning as null.

Help or advice would be greatly appreciated.


Submitted via EggHeadCafe - Software Developer Portal of Choice
DataContractSerializer Basics
http://www.eggheadcafe.com/tutorials/aspnet/ad947ce6-cd3e-4647-b69c-94d2f3b1b265/datacontractserializer-ba.aspx