473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 5461
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: ck388 | last post by:
For some reason when I enable the callback feature of the gridview I still get a page refresh, that is it seems like there is a postback that occurs, not a callback which is just supposed to update...
2
by: Arjen | last post by:
Hi, I get this error message when sorting a gridview: The GridView 'GridView1' fired event Sorting which wasn't handled What do I need to do? Thanks!
4
by: samb | last post by:
When I use manual databinding to a GridView control, as bellow. 'Retrive a DataSet from database Dim ds As DataSet = uda.GetUsers(conectionString) 'gvUsers - The GridView gvUsers.DataSource...
0
by: jobo | last post by:
Hey there, I'm having a problem getting sorting to work. Here's what the GridView looks like: "server" ID="updt1" Mode="Conditional">
2
by: sivagururaja | last post by:
Hi All, How can i sorting the Gridview Columns via the code behind. When i tried to sorting the column it doesn't work. SqlConnection con = new SqlConnection("Connection string");...
4
by: =?Utf-8?B?R2VyaGFyZA==?= | last post by:
I have a vb.net 2.0 app that is loading a GridView with a DataSource that is returned from a function. The definitions in the function are: Dim ReportDS As DataSet = New DataSet Dim...
0
by: ami | last post by:
Hi everyone, I have a question about dynamically adding columns to a gridview. Based on user input (after a button click), some columns are being added to my gridview. What I do: ...
0
by: =?Utf-8?B?QVZM?= | last post by:
Hi, I've a requirement in which I want to use the custom sorting.. I mean I've a dropdown with sortable column names and when the user selects the particular column from dropdown , I need to sort...
1
by: dorandoran | last post by:
The sort on the childgrid is not working; nothing happens when I click on the each column header for sort. (I followed Satay's sample: http://www.codeproject.com/KB/aspnet/EditNestedGridView.aspx)...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.