Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting Gridview w/o DataSource

Status
Not open for further replies.

ccshadow

Programmer
Jan 29, 2004
33
US
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?

Code:
   Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       'Put user code to initialize the page here
       If Not (IsPostBack) Then
       Dim dtUsers As New DataTable

       Try
           Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("WilliganConnectionString").ConnectionString)
           Dim cmd As New SqlCommand("stp_GetUsers", conn)
           conn.Open()
           Dim reader As SqlDataReader = cmd.ExecuteReader()
           If reader.HasRows Then
               dtUsers.Load(reader)
               Dim dvUsers As DataView = dtUsers.DefaultView
               ViewState("SortOrder") = "lastName ASC"

               Session("UsersTable") = dvUsers
               grdUsers.DataSource = dvUsers
               grdUsers.DataBind()
           End If

           reader.Close()
           reader = Nothing
           cmd.Dispose()
           cmd = Nothing
           conn.Dispose()
           conn = Nothing

       Catch ex As Exception
           Trace.Warn(ex.ToString)
       End Try
       Else
           'Only runs on first page load
           grdUsers.HeaderRow.HorizontalAlign = HorizontalAlign.Center
       End If
End Sub

   Protected Sub grdUsers_Sort(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles grdUsers.Sorting
       Dim dvUsers As New DataView()
       dvUsers = Session("UsersTable")

       Dim newSortDirection As String
       If InStr(ViewState("SortOrder").ToString(), e.SortExpression.ToString
()) = 0 Then
           newSortDirection = e.SortExpression.ToString() & " ASC"
       Else
           If InStr(ViewState("SortOrder").ToString(), "ASC") > 0 Then
               newSortDirection = Replace(ViewState("SortOrder").ToString(),
"ASC", "DESC")
           Else
               newSortDirection = Replace(ViewState("SortOrder").ToString(),
"DESC", "ASC")
           End If
       End If
       ViewState("SortOrder") = newSortDirection
       Trace.Warn("viewstate sortorder before sorting" & ViewState
("SortOrder"))
       dvUsers.Sort = ViewState("SortOrder").ToString()
       grdUsers.DataSource = dvUsers
       grdUsers.DataBind()
   End Sub
 
Trace through your code and make sure the viewstate is holding what you expect. It might just be easier to set the sortexpression in a session variable instead
 
I have been checking the trace files. For some reason the grdUsers_Sort seems to trigger twice. The first time it sets the ViewState to 'columnName ASC' and the second time it sets it to 'columnName DESC'. I discovered this because when I clicked a header it was sorting on DESC when it should have been sorting by ASC. It's setting the variable correctly, I just can't figure out why it's firing twice.

Dis try setting it in a Session variable as suggested, but got the exact same results.
 
I was not referring to trace logs, I ment stepping through the code as it executes.
 
Either way I essentially get the same info by virture of the volume of trace statements I entered. The problem comes in because the grdUsers_Sort function executes twice on a header click. Is my code causing this issue or is this to be expected?
 
I assume in the HTML that the editupdate command calls grdUsers_Sort(). is that sub being called anywhere else in code?
 
Actually it's in the onSorting. That was one of the first things I thought - that it was being called somewhere else. I do have a bindGrid() sub that I call on PageLoad, but only when Not IsPostback. And the grdUsers_Sort is only associated with the control in the HTML code. I kept thinking that the grid might be resorted when I bind it to the datatable in grdUsers_Sort. It's frustrating when I step thru to watch it go directly from the last line of the grdUsers_Sort back up to the first line of that same sub.

Currently the page is pretty small - I'm just trying to control the gridview without using a DataSource as I believe this would be necessary when I integrate Ajax in the page and make the transition less laborous. So it's not difficult to track what's going on in there. This is really the first functionality I've added to the page.

Here's the code for the grid.
Code:
        <asp:GridView ID="grdUsers" runat="server" OnSorting="grdUsers_Sort"
            DataKeyNames="agentId"  
            AutoGenerateColumns="False" AllowSorting="true" 
            CssClass="lgGridview" 
            GridLines="both"
            CellPadding="3" HorizontalAlign="Center" HeaderStyle-HorizontalAlign="center">
        <Columns>
            <asp:BoundField DataField="agentId" ControlStyle-CssClass="hidden" ItemStyle-CssClass="hidden" HeaderStyle-CssClass="hidden" FooterStyle-CssClass="hidden" />
            <asp:BoundField DataField="lastName" SortExpression="lastName" HeaderText="Last Name" HeaderStyle-HorizontalAlign="center" ItemStyle-CssClass="lgGridviewItem" />
            <asp:BoundField DataField="firstName" SortExpression="firstName" HeaderText="First Name" HeaderStyle-HorizontalAlign="center" ItemStyle-CssClass="lgGridviewItem" />
            <asp:BoundField DataField="ntLogin" SortExpression="ntLogin" HeaderText="NT Login" HeaderStyle-HorizontalAlign="center" ItemStyle-CssClass="lgGridviewItem" />
            <asp:BoundField DataField="opsComEleVid" SortExpression="opsComEleVid" HeaderText="opsComEleVid" HeaderStyle-HorizontalAlign="center" ItemStyle-CssClass="lgGridviewItem" />
            <asp:BoundField DataField="opsComEleHsd" SortExpression="opsComEleHsd" HeaderText="opsComEleHsd" HeaderStyle-HorizontalAlign="center" ItemStyle-CssClass="lgGridviewItem" />
            <asp:BoundField DataField="agentSup" SortExpression="agentSup" HeaderText="Supervisor" HeaderStyle-HorizontalAlign="center" ItemStyle-CssClass="lgGridviewItem" />
            <asp:BoundField DataField="agentCsgOpid" SortExpression="agentCsgOpid" HeaderText="CSG Opid" HeaderStyle-HorizontalAlign="center" ItemStyle-CssClass="lgGridviewItem" />
            <asp:BoundField DataField="agentAspect" SortExpression="agentAspect" HeaderText="Aspect" HeaderStyle-HorizontalAlign="center" ItemStyle-CssClass="lgGridviewItem" />
            <asp:BoundField DataField="agentSalesNum" SortExpression="agentSalesNum" HeaderText="Sales Num" HeaderStyle-HorizontalAlign="center" ItemStyle-CssClass="lgGridviewItem" />
            <asp:BoundField DataField="teamName" SortExpression="teamName" HeaderText="Team" HeaderStyle-HorizontalAlign="center" ItemStyle-CssClass="lgGridviewItem" />
            <asp:BoundField DataField="skillSet" SortExpression="skillSet" HeaderText="Skill Set" HeaderStyle-HorizontalAlign="center" ItemStyle-CssClass="lgGridviewItem" />
            <asp:BoundField DataField="caeTools" SortExpression="caeTools" HeaderText="caeTools" HeaderStyle-HorizontalAlign="center" ItemStyle-CssClass="lgGridviewItem" />
            <asp:BoundField DataField="nasTeamName"  SortExpression="nasTeamName" HeaderText="nasTeamName" HeaderStyle-HorizontalAlign="center" ItemStyle-CssClass="lgGridviewItem" />
            <asp:BoundField DataField="carNum" SortExpression="carNum" HeaderText="carNum" HeaderStyle-HorizontalAlign="center" ItemStyle-CssClass="lgGridviewItem" />
            <asp:CheckBoxField DataField="active" SortExpression="active" HeaderText="Active" HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="center" />
            
        </Columns>
    
    </asp:GridView>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top