【问题标题】:SortedAscending/Descending Styles not working排序的升序/降序样式不起作用
【发布时间】:2011-06-02 09:53:26
【问题描述】:

我的网格视图中的排序样式有问题,它们根本没有被应用,无论是单元格样式、标题样式、颜色等...

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

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

【问题讨论】:

    标签: c# asp.net sorting gridview


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 尝试了您发布的内容,但当我对列进行排序时,仍然没有任何样式应用于 TH 或 TD。
    猜你喜欢
    • 1970-01-01
    • 2014-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-24
    • 2021-05-17
    相关资源
    最近更新 更多