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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid Edit, Cancel, Paging calls SortCommand...not just limited to

Status
Not open for further replies.

codecomm

Programmer
Feb 14, 2007
121
US
My issue is I'm trying to have a datagrid (ASP.NET 1.1) to allow editing, along with sorting ASC/DESC via column headers.

When I click "Edit", my SQL query ASC goes to DESC, and vice versa. This happens on Cancel, Paging, Delete, etc...I'm trying to limit the "swap" in the order by criterion only when a header (of Template columns) is clicked.

****************I have this in the HTML:

OnSortCommand="SortDG" AllowSorting="True"

***************Code behind to try to limit only to column headers, but not working:

Private Sub DGActivities_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DGActivities.ItemCommand

If e.Item.ItemType = ListItemType.Header Then

If ViewState("DGActGlobalAscDesc") = "" Then

ViewState("DGActGlobalAscDesc") = "ASC"

SwapDirection(ViewState("DGActGlobalAscDesc"))

Else

SwapDirection(ViewState("DGActGlobalAscDesc"))

End If

End If

End Sub



Sub SwapDirection(ByVal viewStateKey As String)

If ViewState(viewStateKey) = "ASC" Then

ViewState(viewStateKey) = "DESC"

ViewState("DGActFieldAscDesc") = "DESC"

Else

ViewState(viewStateKey) = "ASC"

ViewState("DGActFieldAscDesc") = "ASC"

End If

End Sub
 
codecomm: Don't have time at the moment to tweak your problem but do a thorough search here at Tek-tips for Editing Grids - there should be numerous examples - also, go to Google groups and do a quick search.

There should be plenty of examples - this should be a straight forward application of code and shouldn't be a problem. I don't recall viewstate being used in this particular regard but of course it's seems to be a reasonable approach.

Here's a block of code I used a few years ago with .NET 1.1 on a datagrid, perhaps there will be a clue in there for you:
Code:
Sub Sort_Grid(ByVal Sender As Object, ByVal e As DataGridSortCommandEventArgs)
      Dim SortExprs() As String
      Dim CurrentSearchMode As String, NewSearchMode As String
      Dim ColumnToSort As String, NewSortExpr as String
      'Parse the sort expression - delimiter space
      SortExprs = Split(e.SortExpression, " ")
      ColumnToSort = SortExprs(0)
      'If a sort order is specified get it, else default is ascending
      If SortExprs.Length() > 1 Then
        CurrentSearchMode = SortExprs(1).ToUpper()
        If CurrentSearchMode = "ASC" Then
          NewSearchMode = "DESC"
        Else
          NewSearchMode = "ASC"
        End If
      Else  'If no mode specified, Default is ascending
        NewSearchMode = "ASC"
      End If
      'Derive the new sort expression. 
      NewSortExpr = ColumnToSort & " " & NewSearchMode
      Dim iIndex As String
      If ColumnToSort = "AwwSiteCode" Then
        iIndex = 0
      ElseIf ColumnToSort = "Group_Name" Then
        iIndex = 1 
      ElseIf ColumnToSort = "Waterbody_Name" Then
        iIndex = 2
      ElseIf ColumnToSort = "Description" Then
        iIndex = 3 
      ElseIf ColumnToSort = "LastDate" Then
        iIndex = 7
      ElseIf ColumnToSort = "ChemCt" Then
        iIndex = 8 
      ElseIf ColumnToSort = "BacCt" Then
        iIndex = 9
      End If
      lblA.Text = NewSortExpr
     'alter the column's sort expression 
      dgGroups.Columns(iIndex).SortExpression = NewSortExpr
     'Sort the data in new order
      GetSites(NewSortExpr)
    End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top