2012-01-03 45 views
1

有人可以分享如何在實踐中實現GridView的分類和處理,如果該事件:ASP.NET GridView控件實現排序和事件處理

  1. 的數據綁定手動
  2. 的GridView使用模板字段建立了從碼抽僅次於(不加價)

我建立我的GridView單從代碼隱藏所以我不能使用默認的方法或解決方案。

謝謝

+0

您是否想要內置GridView排序功能的示例代碼? – Alok 2012-01-03 06:16:49

+0

你用google搜索了這個。 http://weblogs.asp.net/vikram/archive/2008/04/15/manually-sorting-and-paging-gridview-without-using-datasource-control.aspx – 2012-01-03 06:17:05

+0

http://stackoverflow.com/questions/ 5608049/sorting-gridview-in-asp-net -c – 2012-01-03 06:18:45

回答

5

這可能是你正在尋找:

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) 
{ 
    listBindByName(); //this would be your procedure to look for the data you want 
    DataSet dsSortTable = GridView1.DataSource as DataSet; 
    DataTable dtSortTable = dsSortTable.Tables[0]; 
    if (dtSortTable != null) 
    { 
     DataView dvSortedView = new DataView(dtSortTable); 
     dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString(); 
     ViewState["sortExpression"] = e.SortExpression; 
     GridView1.DataSource = dvSortedView; 
     GridView1.DataBind(); 
    } 
    UpdatePanel1.Update(); 
} 

private string getSortDirectionString() 
{ 
    if (ViewState["sortDirection"] == null) 
    { 
     ViewState["sortDirection"] = "ASC"; 
    } 
    else 
    { 
     if (ViewState["sortDirection"].ToString() == "ASC") 
     { 
      ViewState["sortDirection"] = "DESC"; 
      return ViewState["sortDirection"].ToString(); 
     } 
     if (ViewState["sortDirection"].ToString() == "DESC") 
     { 
      ViewState["sortDirection"] = "ASC"; 
      return ViewState["sortDirection"].ToString(); 
     } 
    } 
    return ViewState["sortDirection"].ToString(); 
} 

這是模板列的例子:

<asp:TemplateField HeaderText="Description" SortExpression="description"> 
    <ItemTemplate> 
     <asp:Label Visible="true" runat="server" ID="descriptionLabel" Text='<%# bind("description") %>'></asp:Label> 
    </ItemTemplate> 
    <EditItemTemplate> 
     <asp:TextBox ID="txtEditDescription" Width="100px" runat="server" Text='<%#Bind("description") %>' /> 
    </EditItemTemplate> 
</asp:TemplateField> 

通過添加SortExpression屬性的GridView的標題將變得可點擊。確保sort expression屬性是通過sql查詢綁定的字段的名稱。

希望這會有所幫助。

+0

我如何在這裏實現類似的問題:https://stackoverflow.com/questions/25148278/why-do-i-get-a-column-not-found-error-while-sorting-a-gridview – SearchForKnowledge 2014-08-06 02:06:22

1
/* Best to use the shortened routine below - which can be further shortened */   
private string GetSortDirectionString() 
{ 
    if (ViewState["sortDirection"] == null) ViewState["sortDirection"] = "ASC"; 

    var currDir = ViewState["sortDirection"].ToString().ToUpper(); 

    switch (currDir) 
    { 
     case "ASC": ViewState["sortDirection"] = "DESC"; break; 
     case "DESC": ViewState["sortDirection"] = "ASC"; break; 
    } 

    return ViewState["sortDirection"].ToString(); 
} 
0

加入排序在ASP.NET的

第一步 添加網格視圖到您的網頁編輯源代碼網格視圖允許排序真實和分揀啓動事件

<asp:GridView ID="GridView1" AllowSorting="true" OnSorting="GridView1_Sorting" runat="server"> 
    </asp:GridView> 

第二步

在頁面後面的代碼中我們需要處理這個事件「GridView1_Sorting」和數據表綁定。 在頁面加載,我們將結合數據表與GridView控件

dt = Class1.getDataSet().Tables[0]; // here dt is the datatable object declared Globally. 
GridView1.DataSource = dt; 
GridView1.DataBind(); 

所以現在,如果我們運行的代碼網格視圖將是可見的,但不排序。

第三步

接下來,我們需要處理GridView的Sorting事件。首先我們需要聲明一個靜態字符串SortDirection。

protected void SetSortDirection(string sortDirection) 
     { 
      if (sortDirection == "ASC") 
      { 
       _sortDirection = "DESC"; 
      } 
      else 
      { 
       _sortDirection = "ASC"; 
      } 
     } 

所以sortDirection是一個靜態的字符串...我們用來ascendin和下降之間切換,此功能... 步驟4

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) 
    { 
     SetSortDirection(SortDirection); 
     if (dt != null) 
     { 
      //Sort the data. 
      dt.DefaultView.Sort = e.SortExpression + " " + _sortDirection; 
      GridView1.DataSource = dt; 
      GridView1.DataBind(); 
      SortDireaction = _sortDirection; 
     } 
    } 

所以我們已經完成了排序.. ..sortExpression不過是列名...