Manually sorting a gridview, am I plain stupid?

K

kurt sune

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
 
F

Flinky Wisty Pomm

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?
 
K

kurt sune

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
 
J

JP.Gantlin

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.?"
 
K

kurt sune

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top