排序的升序/降序样式不起作用

时间:2011-06-02 09:53:27

标签: c# asp.net sorting gridview

我在gridview中遇到排序样式的问题,它们根本没有应用,无论是单元格样式,标题样式,颜色等等......

也许是因为我对数据视图进行排序的方式?这就像框架没有看到列被排序......

protected void dgvOpps_Sorting(object sender, GridViewSortEventArgs e)
{
    string sortDirection = "ASC";
    string lastDirection = ViewState["SortDirection"] as string;

    if ((lastDirection != null) && (lastDirection == "ASC"))
        sortDirection = "DESC";

    ViewState["SortDirection"] = sortDirection;
    ViewState["SortExpression"] = e.SortExpression;

    string orderByCol = e.SortExpression;

    DataTable dt;

    if (Session["dgvOppsFilter"] != null)
        dt = ldcrmClient.RetrieveOpportunitiesOfReseller(loggedUser.account_id, (string[])Session["dgvOppsFilter"], new string[0]);
    else dt = ldcrmClient.RetrieveOpportunitiesOfReseller(loggedUser.account_id, new string[0], new string[0]);

    DataView dv = new DataView(dt);
    dv.Sort = e.SortExpression + " " + sortDirection;

    Session.Add("dgvOppsSort", e.SortExpression + " " + sortDirection);

    dgvOpps.DataSource = dv;
    dgvOpps.DataBind();
}

1 个答案:

答案 0 :(得分:0)

试试这个:

 protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                if (ViewState["sortDirection"] == null)
                    ViewState["sortDirection"] = "asc";

                if (ViewState["sortExpression"] == null)
                    ViewState["sortExpression"] = "your-column-name";
               // Fill the Grid
            }
        }
        catch (Exception ex)
        {
            //Response.Write(ex.Message);
        }
    }

排序:

protected void dgvOpps_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (Convert.ToString(ViewState["sortDirection"]) == "asc")
            ViewState["sortDirection"] = "desc";
        else
            ViewState["sortDirection"] = "asc";


        ViewState["sortExpression"] = e.SortExpression;
// Feetch dt here according to your code
          if (dt.Rows.Count > 0)
            {
                dt.DefaultView.Sort = Convert.ToString(ViewState["sortExpression"]) + " " + Convert.ToString(ViewState["sortDirection"]);
                dgvOpps.DataSource =dt;
                dgvOpps.DataBind();
            }
            else
            {
                dgvOpps.DataSource = null;
                dgvOpps.DataBind();

            }
    }