473,568 Members | 2,850 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 1617

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

Similar topics

1
3519
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 javascript, nothing really having to do with ASP.NET... I'm attempting to put together an application that consists of several GridView controls each...
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 an eventhandler thus: Protected Sub grdResult_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles...
0
1756
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
5965
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 use bound fields. The sort expression is just the same datafield that binds the column, but when I try to sort with the post back, nothing works.
2
5574
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
11133
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
3
22747
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. I turned on the Allow Sorting property but when I click one of the columns that is bound, it will not sort. Below is the code I am using
0
1212
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...
0
950
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 gridview should be sorted... As of now, whenever user selects a value in the dropdown , Im rebinding the grid with sorting applied.... but this is...
1
2940
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" ...
0
7604
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...
0
7916
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. ...
0
7962
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...
0
6275
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5498
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...
0
5217
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2101
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
0
932
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...

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.