2011-06-02 55 views
1
我遇到了我的GridView中排序样式麻烦

风格,他们根本就没有被应用,无论是cellstyle,headerstyle,颜色等...SortedAscending /降序不工作

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

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

回答

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

      } 
    } 
+0

试图正是你贴什么,仍然没有风格当我对列进行排序时,应用于TH或TD。 – francis 2011-06-02 10:57:36