【问题标题】:Sort Datatable/Dataview/Gridview of files based on File Modified Date根据文件修改日期对文件的 Datatable/Dataview/Gridview 进行排序
【发布时间】:2010-08-02 20:01:21
【问题描述】:

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

我目前正在按文件名(通过使用数据视图)对这个表进行排序,这不是很有帮助,并且 我想要按文件创建或文件修改日期排序的文件的网格视图(如记录在Windows)。

如果这不可能,第二个选择是从文件名字符串中提取日期(这样做没问题)并根据它对数据视图/数据表或网格视图进行排序。 示例文件名: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>

【问题讨论】:

  • 请发布您的 Gridview 控件。您使用的是自动生成的字段吗?

标签: .net asp.net vb.net


【解决方案1】:

试试这个新的页面加载。带有“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>

【讨论】:

  • 我希望避免为日期添加另一列,因为日期已包含在文件名中,用户单击该文件名即可打开文件。最坏的情况,这会做。谢谢!
  • @Albert 您可以将另一列作为数据源的一部分而不显示它。只需使用 BoundFields 并关闭 Grid 上的 AutoGenerateColumns。
  • @Albert 如果您不想看到该列,您应该创建一个 并在 gridview 中设置 AutoGenerateColumns="false"。 (见编辑)。
【解决方案2】:

仅出于完整性考虑 - 我建议使用具有(不)可见日期列的 Datatable/Dataview,如 Carters 回答。 但是您也可以将集合用作具有自定义对象和比较器的网格的数据源。我创建了一个小样本来说明我的意思:

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 属性(文件名和日期)将显示为网格中的列,因为它是 FileDate 中唯一的公共属性。 aspx-Page 仅包含一个空的 GridView。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-02
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    • 1970-01-01
    • 2023-02-22
    相关资源
    最近更新 更多