471,592 Members | 1,159 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,592 software developers and data experts.

Manually sorting a gridview, am I plain stupid?

I have a an aspx page with a gridview.
The gridview is data bound to a generic list of custom classes.
The gridview's DataSource is thus not set.

Now I want to add sorting to it.

So I create an eventhandler thus:

Protected Sub grdResult_Sorting(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewSortEventArgs) Handles grdResult.Sorting
Static Sorting As Boolean = False
If Not Sorting Then
Sorting = True
GridViewSortExpression = e.SortExpression
grdResult.Sort(GridViewSortExpression, GetSortDirection)
grdResult.DataBind()
End If
End Sub

The effect is that the gridview doesn't get rendered.
If I comment out the databind it gets rendered but not sorted.
In debug mode I can se that the grid is populated allright.

What am I doing wrong?

/k
Oct 24 '06 #1
4 5376
If I recall correctly, the ObjectDataSource caches data so that when
you sort and page you don't need to rebind to data. If you call
DataBind without providing a DataSource, you will bind to null, and
you'll get no GridView.

If you want to implement custom sorting, you're better to handle the
sorting event, grab your List again and sort it yourself before calling
DataBind.

You can always cache the List if the data isn't changing in between. I
assume that the ODS stores the data in ViewState, so there shouldn't be
any penalty to doing likewise.

Can anyone clarify that for me?
kurt sune wrote:
I have a an aspx page with a gridview.
The gridview is data bound to a generic list of custom classes.
The gri dview's DataSource is thus not set.

Now I want to add sorting to it.

So I create an eventhandler thus:

Protected Sub grdResult_Sorting(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewSortEventArgs) Handles grdResult.Sorting
Static Sorting As Boolean = False
If Not Sorting Then
Sorting = True
GridViewSortExpression = e.SortExpression
grdResult.Sort(GridViewSortExpression, GetSortDirection)
grdResult.DataBind()
End If
End Sub

The effect is that the gridview doesn't get rendered.
If I comment out the databind it gets rendered but not sorted.
In debug mode I can se that the grid is populated allright.

What am I doing wrong?

/k
Oct 24 '06 #2
If you want to implement custom sorting, you're better to handle the
sorting event, grab your List again and sort it yourself before calling
DataBind.
Do you know if I can grab the List again from viewstate
or must I save it there myself, thus having it in there two times?

(I mean serialize my custom classes and persist them in viewstate)

I do not want to fetch the data again from the business layer, just sort the
existing grid with the same old data.

/k
Oct 24 '06 #3
Well it looks like you got your answer to the sorting question.

I think it's about time someone tackled the other question, "are you plain
stupid.?"

Oct 24 '06 #4
Found out that the gridview doesnt support sorting in itself.
So I ended up with a custom web control with a gridview and added support
for clientside sorting by means of javascript.
That also made it possible to eliminate viewstate.

/k

Oct 27 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by samb | last post: by
reply views Thread by jobo | last post: by
2 posts views Thread by sivagururaja | last post: by
4 posts views Thread by =?Utf-8?B?R2VyaGFyZA==?= | last post: by
reply views Thread by ami | last post: by
reply views Thread by =?Utf-8?B?QVZM?= | last post: by
1 post views Thread by dorandoran | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.