2014-01-14 112 views
1

我創建了一個帶有自定義列的GridView並且想對它們執行排序。當我點擊列時,沒有事件正在觸發排序。我想根據gridview的列進行排序,但是當點擊HeaderTemplete的標題時事件並未觸發

下面是我寫

對於.aspx頁面中

<asp:GridView ID="grdConfigureCustomers" runat="server" AlternatingRowStyle- BackColor="Aqua" Width="1300px" 
     OnRowCommand="grdConfigureCustomers_RowCommand" OnRowEditing="grdConfigureCustomers_RowEditing" 
     OnRowUpdating="grdConfigureCustomers_RowUpdating" OnRowCancelingEdit="grdConfigureCustomers_RowCancelingEdit" 
     AutoGenerateColumns="false" AllowPaging="true" PageSize="5" 
     OnRowDeleting="grdConfigureCustomers_RowDeleting" ShowFooter="true" 
     onpageindexchanging="grdConfigureCustomers_PageIndexChanging" OnSorting="grdConfigureCustomers_Sorting" AllowSorting="true"> 
     <HeaderStyle BackColor="AliceBlue" /> 
     <Columns> 
<asp:TemplateField HeaderText="Customer ID"> 
       <ItemTemplate> 
        <asp:Label ID="lblCustomerId" runat="server" Text='<%# Eval("Customer_ID") %>'></asp:Label> 
       </ItemTemplate> 
       <FooterTemplate> 
        <asp:TextBox ID="txtAddCustomerId" runat="server"></asp:TextBox><span style="color:Red;">*</span> 
        </FooterTemplate> 
      </asp:TemplateField> 
</Columns> 
</asp:GridView> 

對於.aspx.cs頁

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

    } 

    private void LoadCustomerConfigurationGridView() 
    { 
     DataTable dt = new DataTable(); 
     dt = sqlHelper.GetCustomerListInformation(); 
     grdConfigureCustomers.DataSource = dt; 
     grdConfigureCustomers.DataBind(); 
     Session["data"] = dt; 

    } 
private void LoadCustomerConfigurationGridView(string srtexpr, string direc) 
    { 
     DataTable dt = new DataTable(); 
     dt = sqlHelper.GetCustomerListInformation(); 
     DataView dv = new DataView(dt); 
     dv.Sort = srtexpr + " " + direc; 
     grdConfigureCustomers.DataSource = dv; 
     grdConfigureCustomers.DataBind(); 
     Session["data"] = dt; 

    } 
protected void grdConfigureCustomers_Sorting(object sender, GridViewSortEventArgs e) 
    { 
     switch (e.SortExpression) 
     { 
      case "DateLogged": 
       if (e.SortDirection == SortDirection.Ascending) 
       { 
        LoadCustomerConfigurationGridView("DateLogged", "ASC"); 
       } 
       else 
       { 
        LoadCustomerConfigurationGridView(); 
       } 

       break; 

     } 
    } 

請讓我知道我錯過了排序的代碼。提前致謝。

+0

指該物品─http://satindersinght.blogspot.in/2013/05/gridview-sorting-on-header-click -with.html –

回答

1

您需要在模板列指定這樣的SortExpression屬性:

<asp:TemplateField HeaderText="Customer ID" SortExpression="Customer_ID"> 
+0

我也試過上面的代碼,但它不工作。 – Raj

+0

您是否嘗試過在'grdConfigureCustomers_Sorting'方法中調試並設置斷點?單擊標題時是否調用該方法? – ekad

+0

是的,我試圖調試,當我把斷點在grdConfigureCustomers_Sorting它不是hiting。 – Raj

0

您需要啓用排序。 EnableSorting="true"在你的asp.net:Gridview中。

+0

我給了AllowSorting =「true」,並且EnableSorting不是gridview的屬性。我沒有得到你。 – Raj

相關問題