473,834 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_Sorti ng(ByVal sender As Object, ByVal e As
System.Web.UI.W ebControls.Grid ViewSortEventAr gs) Handles grdResult.Sorti ng
Static Sorting As Boolean = False
If Not Sorting Then
Sorting = True
GridViewSortExp ression = e.SortExpressio n
grdResult.Sort( GridViewSortExp ression, GetSortDirectio n)
grdResult.DataB ind()
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 5484
If I recall correctly, the ObjectDataSourc e 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_Sorti ng(ByVal sender As Object, ByVal e As
System.Web.UI.W ebControls.Grid ViewSortEventAr gs) Handles grdResult.Sorti ng
Static Sorting As Boolean = False
If Not Sorting Then
Sorting = True
GridViewSortExp ression = e.SortExpressio n
grdResult.Sort( GridViewSortExp ression, GetSortDirectio n)
grdResult.DataB ind()
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
2737
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 not the whole page, but a portion of the page. Strangely enough the URL below http://beta.asp.net/QUICKSTARTV20/aspnet/doc/ctrlref/data/gridview.aspx (VB GridView Paging and Sorting Callbacks example)
2
11127
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
8254
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 = ds gvUsers.DataBind()
0
1771
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
5586
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"); con.Open(); SqlCommand cmd = new SqlCommand(str, con); SqlDataAdapter da = new SqlDataAdapter(cmd);
4
11165
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 ReportTable As System.Data.DataTable = New System.Data.DataTable("SendTo") The ReportTable is populated row by row by data gotten back from the
0
1327
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: OnButtonClick: I call a method that adds the new boundfield
0
1222
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 the grid. For it,Im trying to get the gridView.DataSource() to retrieve the datatable attached to it (Ive earlier binded the gridview in page load in not postback section ) , but gridView.DataSource() always returning null.. Im unable to...
1
2973
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) and i am using object for datasource. please suggest. = = = default.aspx = = <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
0
9651
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10800
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10225
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7762
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5629
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5800
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3987
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3085
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.