1

正常に入力されているグリッドビューがあります。今、私はソートを有効にしたいです。必要なすべてのコードを実行しました。つまり、マークアップでの並べ替えを有効にし、ユーザーが並べ替えたときに呼び出すイベントを提供します。

それは私が失ったソートイベントです-私はGoogleからいくつかの実装を試しましたが、あまりよくわかりません。基本的に、ユーザーが並べ替える列とASCまたはDESCに応じて、サーバーに新しいクエリを提供する必要があると言っていますか?もしそうなら、それはより多くの仕事のように聞こえます....より多くのクエリ。

ありがとうダモ

グリッドをバインドするためのコードビハインド

 // Load the main homepage data to the grid
                    FAServices fServices = new FAServices(sConn);
                    FAAuditOverallStatusLatest fAuditOverallStatusLatest = new FAAuditOverallStatusLatest(sConn);
                    GridViewMain.DataSource = fAuditOverallStatusLatest.getAuditOverallStatusLatest();
                    GridViewMain.DataBind();

ソートするためのコードビハインド

protected void GridViewMain_Sorting(object sender, GridViewSortEventArgs e)
{

    // Switch statements required here along with Query for each column i have in the grid




}

グリッドマークアップ

<asp:GridView ID="GridViewMain" OnRowDataBound="GridViewMainRowDataBound" OnPageIndexChanging="GridViewMain_PageIndexChanging"
                                        runat="server"  AllowPaging="True" PageSize="50" PagerSettings-Position="TopAndBottom"
                                        CssClass="mGrid"
                                        PagerStyle-CssClass="pgr"
                                        AlternatingRowStyle-CssClass="alt data-row"
                                        OnRowCreated="GridViewMain_RowCreated"                          
                                        RowStyle-CssClass="data-row"                                        
                                        AllowSorting="True"
                                        OnSorting="GridViewMain_Sorting"
                                        >
                                     </asp:GridView>
4

2 に答える 2

2

ユーザーが並べ替える列とASCまたはDESCに応じて、サーバーに新しいクエリを提供する必要があると言っているのは正しいですか?もしそうなら、それはより多くの仕事のように聞こえます....より多くのクエリ。

はい、その通りです。新しい並べ替えを適用するには、データソースを再度クエリする必要があります。しかし、最後の文は正しくありません。ORDER BYSQL(または注文に使用するもの)のを変更する必要がありますDataSource。order-columnとdirectionの両方に1つのViewState変数を使用できます(ポストバック間で永続化する必要があるため、ViewState)。例を示します。

まず、SortExpressionを設定する必要があります。

<asp:TemplateField HeaderText="Name" SortExpression="Name">
    <ItemTemplate>
        <a href='sometest.aspx?ID=<%# DataBinder.Eval(Container.DataItem, "TestID").ToString()%>'><%# DataBinder.Eval(Container.DataItem, "Type").ToString()%></a>
    </ItemTemplate> 
</asp:TemplateField>    
<asp:TemplateField HeaderText="Address" SortExpression="Address">
    <ItemTemplate>
                <div align="right"><%# (DataBinder.Eval(Container.DataItem, "HouseNumber"))%></div>
    </ItemTemplate> 
</asp:TemplateField>            

SortExpressionコードビハインドでは、現在とSortDirectionを格納できますViewState

private string SortExpression {
    get {
        if(String.IsNullOrWhiteSpace((String)ViewState["SortExpression"])
            ViewState["SortExpression"] = "Name ASC";

        return (String)ViewState["SortExpression"];
    }
    set { ViewState["SortExpression"] = value; }
}

これがSortingイベントハンドラーです。を設定して呼び出すBindGridメソッドであることに注意してくださいDataSourceGridView.DataBind

protected void theGrid_Sorting(object sender, System.Web.UI.WebControls.GridViewSortEventArgs e)
{
    string currentSortColumn = null;
    string currentSortDirection = null;
    currentSortColumn = this.SortExpression.Split(' ')[0];
    currentSortDirection = this.SortExpression.Split(' ')[1];

    if (e.SortExpression.Equals(currentSortColumn)) {
        //switch sort direction
        switch (currentSortDirection.ToUpper()) {
            case "ASC":
                this.SortExpression = currentSortColumn + " DESC";
                break;
            case "DESC":
                this.SortExpression = currentSortColumn + " ASC";
                break;
        }
    } else {
        this.SortExpression = e.SortExpression + " ASC";
    }

    //load the data with this SortExpression and DataBind the Grid
    BindGrid();
}
于 2012-09-21T22:03:43.147 に答える
1

私は数行でより単純な方法を持っています:(vb.netのコード、c#での翻訳のためにそれは簡単です)

Dim SortDirection As String, SortExpression As String
SortExpression = e.SortExpression
If (SortExpression = ViewState("SortExpression") And ViewState("SortDirection") = "asc") Then
    SortDirection = "desc"
Else
    SortDirection = "asc"
End If
ViewState("SortDirection") = SortDirection
ViewState("SortExpression") = SortExpression

Dim dt As Data.DataTable = ds.Tables(0) '<<<< fill ds with a method
dt.DefaultView.Sort = SortExpression & " " & SortDirection

GridView1.DataSource = dt.DefaultView
GridView1.DataBind()

それで全部です。このコードをジェネリッククラスに配置して、他のグリッドビューに適用するのは簡単です。

于 2013-04-25T13:26:39.073 に答える