473,503 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sorting Gridview w/o DataSource

4 New Member
I have a Gridview populated with a DataTable that I store in the Session.
I'm trying to sort the columns, but have only been able to get it to
partially work. When I click on a header the first time it sorts as expected,
but thereafter doesn't do anything. I'm attempting to store the current sort
order in ViewState in order to determine what the order should be on a header
click. Can anyone see what I'm doing wrong?

Expand|Select|Wrap|Line Numbers
  1.    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.
  2. EventArgs) Handles MyBase.Load
  3.        'Put user code to initialize the page here
  4.        If Not (IsPostBack) Then
  5.        Dim dtUsers As New DataTable
  6.  
  7.        Try
  8.            Dim conn As New SqlConnection(ConfigurationManager.
  9. ConnectionStrings("WilliganConnectionString").ConnectionString)
  10.            Dim cmd As New SqlCommand("stp_GetUsers", conn)
  11.            conn.Open()
  12.            Dim reader As SqlDataReader = cmd.ExecuteReader()
  13.            If reader.HasRows Then
  14.                dtUsers.Load(reader)
  15.                Dim dvUsers As DataView = dtUsers.DefaultView
  16.                ViewState("SortOrder") = "lastName ASC"
  17.  
  18.                Session("UsersTable") = dvUsers
  19.                grdUsers.DataSource = dvUsers
  20.                grdUsers.DataBind()
  21.            End If
  22.  
  23.            reader.Close()
  24.            reader = Nothing
  25.            cmd.Dispose()
  26.            cmd = Nothing
  27.            conn.Dispose()
  28.            conn = Nothing
  29.  
  30.        Catch ex As Exception
  31.            Trace.Warn(ex.ToString)
  32.        End Try
  33.        Else
  34.            'Only runs on first page load
  35.            grdUsers.HeaderRow.HorizontalAlign = HorizontalAlign.Center
  36.        End If
  37. End Sub
  38.  
  39.    Protected Sub grdUsers_Sort(ByVal sender As Object, ByVal e As System.Web.
  40. UI.WebControls.GridViewSortEventArgs) Handles grdUsers.Sorting
  41.        Dim dvUsers As New DataView()
  42.        dvUsers = Session("UsersTable")
  43.  
  44.        Dim newSortDirection As String
  45.        If InStr(ViewState("SortOrder").ToString(), e.SortExpression.ToString
  46. ()) = 0 Then
  47.            newSortDirection = e.SortExpression.ToString() & " ASC"
  48.        Else
  49.            If InStr(ViewState("SortOrder").ToString(), "ASC") > 0 Then
  50.                newSortDirection = Replace(ViewState("SortOrder").ToString(),
  51. "ASC", "DESC")
  52.            Else
  53.                newSortDirection = Replace(ViewState("SortOrder").ToString(),
  54. "DESC", "ASC")
  55.            End If
  56.        End If
  57.        ViewState("SortOrder") = newSortDirection
  58.        Trace.Warn("viewstate sortorder before sorting" & ViewState
  59. ("SortOrder"))
  60.        dvUsers.Sort = ViewState("SortOrder").ToString()
  61.        grdUsers.DataSource = dvUsers
  62.        grdUsers.DataBind()
  63.    End Sub
Nov 15 '06 #1
0 1614

Sign in to post your reply or Sign up for a free account.

Similar topics

1
3515
by: jmdolinger | last post by:
Hi all, I'm a newbie to Atlas (and recently ASP.NET) after coming from a long Java background, also have done quite a bit with an Ajax.NET/ASP.NET 1.1 project, but it was basically all...
4
5469
by: kurt sune | last post by:
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...
0
1755
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">
5
5959
by: Cindy Lee | last post by:
I'm getting my data from an XML file. The data binds fine, but I can't sort on it. Do I have to do anything special? I have enabled sorting on my grid view. Autogenerate columns is off, and I...
2
5572
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
11122
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...
3
22740
by: =?Utf-8?B?YmJkb2J1ZGR5?= | last post by:
I have a question that maybe somebody can help me out. I have a gridview that is bound to a sqltable, and I have created two template columns. I am having problems getting the sorting to work....
0
1205
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...
0
944
by: =?Utf-8?B?QVZM?= | last post by:
Hi I want to implment cusotm sorting in gridview... Ive a dropdown box on my page where ive all the fields(columsn) to be sorted.. whenever hte user selects the value in the dropdown , the...
1
2937
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
7198
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
7072
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...
0
7319
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
7449
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...
0
5570
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,...
1
4998
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...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
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...
0
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.