Tech Off Thread

5 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

problem with gridview sorting in BOTH direction

Back to Forum: Tech Off
  • User profile image
    kalchev

    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[/CODE]

    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

  • User profile image
    Halvor

    When you debug and add a watch at "e.SortExpression", does it change at all ? I am not yet that familiar with GridView, but i see you change e.SortDirection. Does that also change e.SortExpression?
    Try set "dv.Sort = e.SortExpression" in the if/else instead of the e-expression. This is just my first thoughts. dont have software here to try myself..

  • User profile image
    kalchev

    It does not change and I do not know how to change it. When I put the e.sort... in the If  - the same reasul. Actually I've noticed that when I put EnableSortingAndPagingCallbacks="True" in the gridview no kind of sorting does work. But that is not the main problem. I just need to make it work in both direction - Acc and desc

  • User profile image
    csharp1171

    Try using ViewState to track the sotring:

    if(ViewState["SortExp"].ToString().ToUpper() == e.SortExpression.ToUpper())
    {
     if(ViewState["SortDirection"].ToString().ToUpper() == " ASC")
      ViewState["SortDirection"] = " DESC";
     else
      ViewState["SortDirection"] = " ASC";
    }
    else
    {
     ViewState["SortExp"] = e.SortExpression;
     ViewState["SortDirection"] = " ASC";
    }

    dv.Sort = ViewState["SortExp"].ToString() + " " + ViewState["SortDirection"].ToString();

    dgDataGrid.DataSource = dv;
    dgDataGrid.DataBind();

  • User profile image
    kalchev

    thank you guys. I've found the solution(by Ryan Olshan) here it is:

    #region Database Code
    private void PopulatePublishersGridView()
    {
    string m_Access_Connection_String = AccessConnectionString();
    OleDbConnection m_Access_Connection = new OleDbConnection(m_Access_Connection_String);

    string m_Sql_Query_Select;
    m_Sql_Query_Select = "SELECT [PubID], [Name], [Company Name], [Address], [City], [State], [Zip], [Telephone], [Fax], [Comments] FROM Publishers ORDER BY [Name] ASC;";

    OleDbCommand m_Access_Command = new OleDbCommand(m_Sql_Query_Select, m_Access_Connection);

    OleDbDataAdapter m_DataAdapter_Publishers = new OleDbDataAdapter(m_Access_Command);
    DataTable m_DataTable_Publishers = new DataTable("Publishers");
    m_DataAdapter_Publishers.Fill(m_DataTable_Publishers);

    int m_DataTable_RowCount;
    m_DataTable_RowCount = m_DataTable_Publishers.Rows.Count;

    if (m_DataTable_RowCount > 0)
    {
    gridViewPublishers.DataSource = m_DataTable_Publishers;
    gridViewPublishers.DataBind();
    }
    }

    private string AccessConnectionString()
    {
    string m_Access_DatabasePath = Server.MapPath("~/App_Data/biblio.mdb");
    return String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};", m_Access_DatabasePath);
    }
    #endregion

    #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

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.