2

ASP.NET と C# を使用しています。これが私のコードです。

    private const string ASCENDING = "ASC"; 
    private const string DESCENDING = "DESC";
    public SortDirection GridViewSortDirection 
    {     
        get     
        {         
            if (ViewState["sortDirection"] == null)             
                ViewState["sortDirection"] = SortDirection.Ascending;          
            return (SortDirection) ViewState["sortDirection"];                     
        }     
        set { ViewState["sortDirection"] = value; }  
    }

    public string SortExpression
    {
        get
        {
            if (ViewState["sortExpression"] == null)
                ViewState["sortExpression"] = "JobNumber";
            return ViewState["sortExpression"] as string;
        }
        set { ViewState["sortExpression"] = value; }
    }
    protected void OnSorting(object sender, GridViewSortEventArgs e)
    {
            SortExpression = e.SortExpression;
            if (GridViewSortDirection == SortDirection.Ascending)
            {
                GridViewSortDirection = SortDirection.Descending;
            }
            else
            {
                GridViewSortDirection = SortDirection.Ascending;
            }
            BindGrid();
    }

すべての列に並べ替えを適用し、正常に動作しています。しかし、日付列では、この順序(dd / mm / yyyy)のようです。

  • 2012/11/30
  • 2012/10/12
  • 2012 年 9 月 10 日

    <asp:BoundField DataField="ReportedDate" HeaderText="Reported Date" SortExpression="ReportedDate" DataFormatString="{0:DD/MM/YYYY}" HtmlEncode="false" />
    

この列のデータ型は日付です。

これを行う方法?私は間違っていますか?

4

2 に答える 2

6

2つのオプションがあります

1.SQLレベルでソートするには、これが最善かつ適切な方法です。結果セットをグリッドビューにバインドします。

2.DataTable をクエリ出力にバインドし、データテーブルで並べ替えを実行してから、それを gridview にバインドします。データ型をデータテーブル列に追加することに注意してくださいaccTable.Columns.Add("Date",typeof(DateTime));

于 2012-12-11T10:23:46.800 に答える
1

データベース内のこの列のデータ型は何ですか? DateTime フィールドではなく文字列フィールドのようです。この場合は、最初にデータ型を修正する必要があります。その後、グリッドビューで何も変更せずに正しい並べ替え順序を取得できます。

変更してみる

<asp:BoundField DataField="ReportedDate" HeaderText="Reported Date" SortExpression="ReportedDate" DataFormatString="{0:DD/MM/YYYY}" HtmlEncode="false" />

<asp:TemplateField SortExpression="ReportedDate">
    <ItemTemplate>
            <asp:label id="lblDate" runat="server" text='<%# Eval("ReportedDate", "{0:DD/MM/YYYY}") %>' />
    </ItemTemplate></asp:TemplateField>
于 2012-12-11T10:04:24.830 に答える