根据文件修改日期对文件的Datatable / Dataview / Gridview进行排序

时间:2010-08-02 20:01:22

标签: .net asp.net vb.net

我有一个页面,它使用数据表和gridview列出特定文件夹中的所有文件(所有PDF文件)。

我目前正在按照文件名(通过使用数据视图)对此表进行排序,这没有用,并且我希望按创建的文件或文件修改日期排序的文件的gridview(如记录在窗口)。

如果那是不可能的,第二个选项是从文件名字符串中提取日期(没问题)并根据该选项对dataview / datatable或gridview进行排序即可。 示例文件名:DailySalesReport-1-15-2010。我唯一的挂断是如何对日期进行排序,当它是字符串值时?转换为日期?如何根据此转换值对整个数据集进行排序?

感谢您的任何想法!

Protected Sub PageLoad(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then
        Dim dt As New DataTable()
        dt.Columns.Add("Daily Reports", Type.[GetType]("System.String"))

        For Each name As [String] In System.IO.Directory.GetFiles(Server.MapPath("~\reports\pdf\")) '"
                dt.Rows.Add(New Object() {name})
        Next

        Dim dv As DataView = dt.DefaultView
        dv.Sort = dt.Columns(0).ToString + " " + "desc"
        dt = dv.ToTable

        Me.gvDaily.DataSource = dt
        Me.gvDaily.DataBind()

    End If
End Sub



Protected Sub gvDaily_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim hl As New HyperLink()
        hl.NavigateUrl = "~\reports\pdfs\" + e.Row.Cells(0).Text '"
        hl.Text = "Daily Report"
        e.Row.Cells(0).Text = ""
        e.Row.Cells(0).Controls.Add(hl)
    End If

End Sub


<asp:GridView ID="gvDaily" runat="server" Height="80px" Width = "180px" CssClass="tableText"    
          OnRowDataBound="gvDaily_RowDataBound">
          <RowStyle HorizontalAlign="center" />                
</asp:GridView>

2 个答案:

答案 0 :(得分:2)

尝试此新页面加载。使用“FileDate”列。

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Dim dt As New Data.DataTable()
        dt.Columns.Add("Daily Reports", Type.[GetType]("System.String"))
        dt.Columns.Add("FileDate", GetType(System.DateTime))

        For Each name As [String] In System.IO.Directory.GetFiles(Server.MapPath("~\reports\pdf\"))

            Dim fi As New System.IO.FileInfo(name)

            dt.Rows.Add(New Object() {name, fi.LastWriteTime})
        Next

        Dim dv As DataView = dt.DefaultView
        dv.Sort = dt.Columns("FileDate").ColumnName & " " & "desc"
        dt = dv.ToTable

        Me.gvDaily.DataSource = dt
        Me.gvDaily.DataBind()

    End If
End Sub

要仅显示所需的列,请将其用作Gridview。

<asp:GridView ID="gvDaily" runat="server" Height="80px" Width = "180px" CssClass="tableText"    
          OnRowDataBound="gvDaily_RowDataBound" AutoGenerateColumns="false">
          <RowStyle HorizontalAlign="center" />
    <Columns>
        <asp:BoundField DataField="Daily Reports" HeaderText="Daily Report" />
    </Columns>               
</asp:GridView>

答案 1 :(得分:0)

仅为了完整性 - 我建议使用带有(in)可见日期列的数据表/数据视图,如Carters答案。 但您也可以使用Collection作为数据源,用于Grid和Custom对象以及Comparer。我创建了一个小样本,以明确我的意思:

Partial Public Class WebForm1
    Inherits System.Web.UI.Page

    Private Sub WebForm1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            BindData()
        End If
    End Sub

    Private Sub BindData()
        Dim usCulture As New Imports System.GlobalizationCultureInfo("en-US")
        System.Threading.Thread.CurrentThread.CurrentCulture = usCulture
        Dim nextDate As Date = New Date(2010, 1, 15)
        Dim files As New List(Of FileDate)
        Dim rnd As New Random(Date.Now.Millisecond)
        For i As Int32 = 1 To 100
            Dim fileName As String = "DailySalesReport" & i
            files.Add(New FileDate(fileName, nextDate))
            nextDate = nextDate.AddDays(rnd.Next(-10, 10))
        Next
        files.Sort(New FileComparer(SortDirection.Descending))
        Me.GridView1.DataSource = files
        Me.GridView1.DataBind()
    End Sub
End Class

Class FileDate
    Public FileName As String
    Public FileDate As Date

    Public Sub New(ByVal FileName As String, ByVal FileDate As Date)
        Me.FileName = FileName
        Me.FileDate = FileDate
    End Sub

    Public ReadOnly Property Text() As String
        Get
            Return Me.ToString
        End Get
    End Property

    Public Overrides Function ToString() As String
        Return FileName & "-" & FileDate.ToShortDateString
    End Function
End Class

Class FileComparer
    Implements IComparer(Of FileDate)

    Public Direction As SortDirection

    Public Sub New(ByVal direction As SortDirection)
        Me.Direction = direction
    End Sub

    Public Function Compare(ByVal x As FileDate, ByVal y As FileDate) As Integer Implements System.Collections.Generic.IComparer(Of FileDate).Compare
        If x Is Nothing Then
            If y Is Nothing Then
                Return 0
            Else
                Return -1
            End If
        Else
            If y Is Nothing Then
                Return 1
            Else
                If Me.Direction = SortDirection.Ascending Then
                    Return x.FileDate.CompareTo(y.FileDate)
                Else
                    Return y.FileDate.CompareTo(x.FileDate)
                End If
            End If
        End If
    End Function
End Class

在此示例中,只有FileDate的Text属性(文件名和日期)将在Grid中显示为列,因为它是FileDate中唯一的公共属性。 aspx-Page只包含一个空的GridView。