2012-10-05 92 views
0

我從互聯網上得到了下面的代碼。它工作正常。我也加了分頁。當我正在分揀時,它正在正常工作。當我更改頁面索引時,排序會丟失。GridView在分頁後丟失排序?

以下是客戶端代碼,它使用鏈接到服務器端代碼中的「GridView1_Sorting」方法的排序將每頁中的20個項目設置爲gridview。

客戶端

<asp:GridView ID="GridView1" runat="server" DataKeyNames="eno" AutoGenerateColumns="False" PageSize="20" AllowPaging="True" AllowSorting="True" OnSorting="GridView1_Sorting" CellPadding="4"> 
    <Columns> 
     <asp:TemplateField HeaderText="Employee no" SortExpression="eno"> 
      <ItemTemplate> 
       <%#Eval("eno")%> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Emp name" SortExpression="empname"> 
      <ItemTemplate> 
       <%#Eval("empname")%> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Salary" SortExpression="sal"> 
      <ItemTemplate> 
       <%#Eval("sal")%> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

而且現在的服務器端代碼:

服務器端

using System.Data.SqlClient; 
using System.Configuration; 
using System.IO; 

public partial class _Default : System.Web.UI.Page 
{ 
    SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString); 
    SqlCommand sqlcmd; 
    SqlDataAdapter da; 
    DataTable dt = new DataTable(); 
    DataTable dt1 = new DataTable(); 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!Page.IsPostBack) 
     { 
      GridData(); 
     } 
    } 

    void GridData() 
    { 
     sqlcmd = new SqlCommand("select * from emp", sqlcon); 
     sqlcon.Open(); 
     da = new SqlDataAdapter(sqlcmd); 
     dt.Clear(); 
     da.Fill(dt); 

     if (dt.Rows.Count > 0) 
     { 
      Session["dt"] = dt; 
      GridView1.DataSource = dt; 
      GridView1.DataBind(); 
     } 
    } 

    private string GVSortDirection 
    { 
     get { return ViewState["SortDirection"] as string ?? "DESC"; } 
     set { ViewState["SortDirection"] = value; } 
    } 

    private string GetSortDirection() 
    { 
     switch (GVSortDirection) 
     { 
      case "ASC": 
       GVSortDirection = "DESC"; 
       break; 

      //assign new direction as ascending order 
      case "DESC": 
       GVSortDirection = "ASC"; 
       break; 
     } 

     return GVSortDirection; 
    } 

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) 
    { 
     DataTable dataTable = (DataTable)Session["dt"]; 
     if (dataTable != null) 
     { 
      DataView dataView = new DataView(dataTable); 
      string sortDirection = GetSortDirection(); 
      dataView.Sort = e.SortExpression + " " + sortDirection; 
      GridView1.DataSource = dataView; 
      GridView1.DataBind(); 
     } 
    } 

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) 
    { 
     GridView1.PageIndex = e.NewPageIndex; 
     GridData(); 
    } 
} 

回答

1

當你頁面,PageIndexChanging事件被調用。這反過來運行GridData()過程,該過程將gridview的數據源設置爲包含emp表的記錄的數據表,而不需要特定的排序順序。

你應該做的是獲取你在GridView1_Sorting事件處理程序中編寫的代碼,並將其包含在GridData例程中,以便每當網格填充數據時 - 無論頁面首次加載時,當頁面索引被改變或者當gridview被排序時 - gridview是基於排序的數據視圖,而不是未排序的數據表。

0

看一看這個article,可能你會得到你想要

1

您維護使用GetSortDirection SortDirection的方式是什麼,以同樣的方式維持的SortExpression。

快樂編碼!