开发者

Sorting gridview

开发者 https://www.devze.com 2023-01-21 11:33 出处:网络
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.

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 = 开发者_Python百科"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();
    }
}


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();
    }
}


Maybe this could help if your sortdirection is always ascending.


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;
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消