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

Problem with sorting in both direction in GridView

Status
Not open for further replies.

neegottlob

Programmer
Mar 26, 2006
22
BG
Hello,
this is my code:

Code:
<script runat="server">
    Dim authors As New DataSet()
     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        
       ............
    End Sub
   

    Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs)
        ' Retrieve the data source from session state.
        Dim dt As DataTable = CType(Session("Sorting"), DataTable)

        ' Create a DataView from the DataTable.
        Dim dv As DataView = New DataView(dt)
        If e.SortDirection = SortDirection.Ascending Then
            e.SortDirection = SortDirection.Descending
        Else
            e.SortDirection = SortDirection.Ascending
        End If

        dv.Sort = e.SortExpression

       
        GridView1.DataSource = dv
        GridView1.DataBind()
    End Sub

The Problem is that when I click the Header text It always Sort it by Ascending and never reverse it. So I've tried to debug and when I changed manuale the value of SortDirection to descenting it still kept sorting it only ascendint. So is there any posobility to change the direction of the sorting.

Thank You,
G. Kalchev
 
G. Kalchev - much of the code for DataGrid seems to be very close to code for GridViews (I think only a version diff?). Anyway, here is some sorting code for a DataGrid; perhaps you could adopt something in it that might help you:
Code:
Sub Sort_Grid(ByVal Sender As Object, ByVal e As DataGridSortCommandEventArgs)
      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 descending
        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 = 6
      ElseIf ColumnToSort = "ChemCt" Then
        iIndex = 7
      ElseIf ColumnToSort = "BacCt" Then
        iIndex = 8
      End If
      lblA.Text = NewSortExpr
     'alter the column's sort expression 
      dgSites.Columns(iIndex).SortExpression = NewSortExpr
     'Sort the data in new order
      GetSites(NewSortExpr)
 End Sub
 
No need to write code in the sort method. Just click the link to sort, and it will be ASC, click again, and it will be DESC. That is the nice thing about the gridview as opposed to the datagrid
 
thanks, Isadore.. i've solved it:

Code:
#region GridView Code
    private string GridViewSortDirection
    {
        get { return ViewState["SortDirection"] as string ?? "DESC"; }
        set { ViewState["SortDirection"] = value; }
    }

    private string GetSortDirection()
    {
        switch (GridViewSortDirection)
        {
            case "ASC":
                GridViewSortDirection = "DESC";
                break;

            case "DESC":
                GridViewSortDirection = "ASC";
                break;
        }

        return GridViewSortDirection;
    }

    protected void gridViewPublishers_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gridViewPublishers.PageIndex = e.NewPageIndex;
        gridViewPublishers.DataBind();
    }

    protected void gridViewPublishers_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable m_DataTable = gridViewPublishers.DataSource as DataTable;

        if (m_DataTable != null)
        {
            int m_PageIndex = gridViewPublishers.PageIndex;

            string m_SortDirection = GetSortDirection();

            DataView m_DataView = new DataView(m_DataTable);
            m_DataView.Sort = e.SortExpression + " " + m_SortDirection;

            gridViewPublishers.DataSource = m_DataView;
            gridViewPublishers.DataBind();
            gridViewPublishers.PageIndex = m_PageIndex;
        }
    }
    #endregion

    #region Page Code
    protected void Page_Load(object sender, EventArgs e)
    {
        PopulatePublishersGridView();
    }
    #endregion
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top