Sorting gridview

I have a gridview where I bind a datasource, and I had to add sorting for this gridview; I added the code below to that, but it didn’t work well.

private string ConvertSortDirectionToSql(SortDirection sortDireciton)
{
    string m_SortDirection = String.Empty;

    switch (sortDireciton)
    {
        case SortDirection.Ascending:
            m_SortDirection = "ASC";
            break;

        case SortDirection.Descending:
            m_SortDirection = "DESC";
            break;
    }

    return m_SortDirection;
}

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

    if (m_DataTable != null)
    {
        DataView m_DataView = new DataView(m_DataTable);
        m_DataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

        GridView1.DataSource = m_DataView;
        GridView1.DataBind();
    }
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can use this, as I had the same problem, and I solved it like this.

public string SortingExpression
{
    get
    {
        if (this.ViewState["SortExpression"] == null)
            return "";
        else
            return (string)this.ViewState["SortExpression"];
    }

    set
    {
        this.ViewState["SortExpression"] = value;
    }
}

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

    if (m_DataTable != null)
    {
        DataView m_DataView = new DataView(m_DataTable);
        SortingExpression = e.SortExpression + " " + (SortingExpression.Contains("ASC") ? "DESC" : "ASC");
        m_DataView.Sort =SortingExpression;

        GridView1.DataSource = m_DataView;
        GridView1.DataBind();
    }
}

Method 2

Maybe this could help if your sortdirection is always ascending.

Method 3

Try this out. This method worked for me.

dt is the datatable containing the values.

 protected void onSorting_Gridview1(object sender, GridViewSortEventArgs e)
            {
                string _sortDirection = dir.ToString();
                if(_sortDirection.Equals("Ascending"))
                {
                _sortDirection = "ASC";
                dir = SortDirection.Descending;

            }
            else
            {
                _sortDirection="DESC";
                dir = SortDirection.Ascending;

            }

            if (dt != null)
            {
                //Sort the data.
                dt.DefaultView.Sort = e.SortExpression + " " + _sortDirection;
                gridView1.DataSource = dt;
                gridView1.DataBind();
            }

        }


 public SortDirection dir
    {
        get
        {
            if (ViewState["DIR"] == null)
            {
               ViewState["DIR"] = SortDirection.Ascending;
            }
            return (SortDirection)ViewState["DIR"];
        }
        set
        {
           ViewState["DIR"] = value;
        }
    }


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x