2014-06-20 27 views
0

我想在用戶單擊列標題時按列排序網格視圖。這裏用戶可以點擊任何列,網格視圖根據點擊的列進行排序。這是我的代碼:如何在列的標題點擊時在網格視圖中排序記錄

<asp:GridView ID="gvEmployeeStatus" runat="server" AutoGenerateColumns="false" AllowPaging="True" PageSize="10" AllowSorting="true" OnPageIndexChanging="gvEmployeeStatus_PageIndexChanging" OnSorting="gvEmployeeStatus_Sorting" > 

protected void gvEmployeeStatus_Sorting(object sender, GridViewSortEventArgs e) 
    { 
     loginName = (String)(Session["LoginName"]); 
     dsLoginDetail = clsBLogic.TblLogin(loginName);     
     tblEmployeeNo = dsLoginDetail.Tables[0].Rows[0]["EmployeeNo"].ToString(); 
     BindDataTogvEmployeeStatus(tblEmployeeNo); 

     DataTable dataTable = gvEmployeeStatus.DataSource as DataTable; 
     if(dataTable != null) 
     { 
      DataView dataView = new DataView(dataTable); 
      dataView.Sort = e.SortExpression + " " + ConvertSortDirection(e.SortDirection); 
      gvEmployeeStatus.DataSource = dataView; 
      gvEmployeeStatus.DataBind(); 
     } 

    } 

    private string ConvertSortDirection(SortDirection sortDirection) 
    { 
     string newSortDirection = String.Empty; 

     switch (sortDirection) 
     { 
      case SortDirection.Ascending: 
       newSortDirection = "ASC"; 
       break; 

      case SortDirection.Descending: 
       newSortDirection = "DESC"; 
       break; 
     } 

     return newSortDirection; 
    } 

網格視圖中的記錄來自數據集。現在,當我運行代碼時,沒有任何反應。我在gvEmployeeStatus_Sorting事件中設置了斷點來檢查它何時被觸發。它並沒有被解僱。我如何排序記錄!

回答

0

爲u提到排序不會被觸發,我猜你缺少的屬性SortExpression<asp:TemplateField> or <asp:BoundField>

例如,

<asp:TemplateField HeaderText="Your Header 1" SortExpression="ColumName1"> 

<asp:BoundField DataField="Your Header 1" HeaderText="Your Header 1" SortExpression="ColumName1" /> 
+0

我已經包含的SortExpression。 。標題文本帶下劃線,頁面回發,但記錄不排序。 –

+0

@AgustusCodes:我可以知道,控制進入'gvEmployeeStatus_Sorting()'功能嗎? – Developer

+0

我在這裏持懷疑態度。爲了測試這個,我在這個函數中放置了斷點,但是斷點沒有進入這裏? –

相關問題