'Gridview1' fired event Sorting which wasn't handled

  • Thread starter Thread starter Vincent
  • Start date Start date
V

Vincent

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
 
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"

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
}
 
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' ...?
 
Normally, when creating a gridview bound to a sqldatasource, the sorting /
paging occur automatically.

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
 
Thanks, i'll read it

Mark Rae said:
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
 
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 said:
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

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

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#...
i tried several things (using string() instead of Datatable ...) but could
not find the solution.

Try this:
Dim dt As DataTable = DirectCast(Gridview1.DataSource, DataTable)
 
Thanks again, but i still get the same error at line:
Dim dt As DataTable = DirectCast(Gridview1.DataSource, DataTable)



Mark Rae said:
Mark Rae said:
Normally, when creating a gridview bound to a sqldatasource, the
sorting / paging occur automatically.

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

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

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#...
i tried several things (using string() instead of Datatable ...) but
could not find the solution.

Try this:
Dim dt As DataTable = DirectCast(Gridview1.DataSource, DataTable)
 
[top-posting corrected]
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

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#...
i tried several things (using string() instead of Datatable ...) but
could not find the solution.

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

Thanks again, but i still get the same error at line:
Dim dt As DataTable = DirectCast(Gridview1.DataSource, DataTable)

Hmm - OK... What datatype is GridView1.DataSource? Can it even be cast to a
DataTable type...?
 
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 said:
[top-posting corrected]
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

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

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

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

Thanks again, but i still get the same error at line:
Dim dt As DataTable = DirectCast(Gridview1.DataSource, DataTable)

Hmm - OK... What datatype is GridView1.DataSource? Can it even be cast to
a DataTable type...?
 
[top-posting corrected again]
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()

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
 
ok, thanks

Mark Rae said:
[top-posting corrected again]
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()

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
 
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 = rolesArra
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 hel
Vincent

Previous Posts In This Thread:

'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 = rolesArra
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 hel
Vincent

Re: 'Gridview1' fired event Sorting which wasn't handled

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 somethin


protected void Gridview1_Sorting(object sender, GridViewSortEventArgs e

// do somethin


--
Mark Ra
ASP.NET MV
http://www.markrae.net

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 wit
// do somethin
Does it exist something like 'sort' or 'paging' ...

Mark Rae [MVP]" <[email protected]> schreef in bericht

Re: 'Gridview1' fired event Sorting which wasn't handled

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 = rolesArra
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.asp

--
Mark Ra
ASP.NET MV
http://www.markrae.net

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

"Mark Rae [MVP]" <[email protected]> schreef in bericht

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

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]" <[email protected]> schreef in bericht

Re: 'Gridview1' fired event Sorting which wasn't handled


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

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]" <[email protected]> schreef in bericht

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

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]" <[email protected]> schreef in bericht

Re: 'Gridview1' fired event Sorting which wasn't handled

[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

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

"Mark Rae [MVP]" <[email protected]> schreef in bericht

'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/tutorial...c-94d2f3b1b265/datacontractserializer-ba.aspx
 
Back
Top