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

.net 根据文件修改日期对文件的Datatable/Dataview/Gridview进行排序,.net,asp.net,vb.net,.net,Asp.net,Vb.net,我有一个页面,使用数据表和gridview列出特定文件夹(所有PDF)中的所有文件 我目前正在按文件名(使用数据视图)对该表进行排序,这没有多大帮助,我希望文件的gridview按文件创建或文件修改日期(如Windows中记录的)进行排序。 如果不可能,第二个选项是从文件名字符串中提取日期(这样做没有问题),并根据该日期对dataview/datatable或gridview进行排序。 文件名示例:DailySalesReport-1-15-2010。我唯一的问题是,当日期是一个字符串值时,我如

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

我目前正在按文件名(使用数据视图)对该表进行排序,这没有多大帮助,我希望文件的gridview按文件创建或文件修改日期(如Windows中记录的)进行排序。

如果不可能,第二个选项是从文件名字符串中提取日期(这样做没有问题)并根据该日期对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>
Protected Sub PageLoad(ByVal sender作为对象,ByVal e作为System.EventArgs)处理Me.Load
如果不是的话,我会回来的
Dim dt作为新数据表()
Add(“每日报告”,键入。[GetType](“System.String”))
对于System.IO.Directory.GetFiles(Server.MapPath(“~\reports\pdf\”)中以[String]形式显示的每个名称”
添加(新对象(){name})
下一个
作为数据视图的Dim dv=dt.DefaultView
dv.Sort=dt.Columns(0).ToString++++“desc”
dt=dv.ToTable
Me.gvDaily.DataSource=dt
Me.gvDaily.DataBind()
如果结束
端接头
受保护的Sub-gvDaily_RowDataBound(ByVal sender作为对象,ByVal e作为GridViewRowEventArgs)
如果e.Row.RowType=DataControlRowType.DataRow,则
Dim hl作为新的超链接()
hl.NavigateUrl=“~\reports\pdfs\”+e.Row.Cells(0.Text')”
hl.Text=“每日报告”
e、 行。单元格(0)。Text=“”
e、 行。单元格(0)。控件。添加(hl)
如果结束
端接头

尝试此新页面加载。带有“FileDate”列

要仅显示所需的列,请将其用作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>

仅为完整起见-我建议使用带有(in)可见日期列(如Carters answer)的Datatable/Dataview。 但您也可以使用集合作为网格的数据源,并使用自定义对象和比较器。我创建了一个小样本,以明确我的意思:

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的文本属性(filename和date)将在网格中显示为列,因为它是FileDate中唯一的公共属性。aspx页面只包含一个空的GridView

请发布您的Gridview控件。您使用的是自动生成的字段吗?我希望避免为日期添加另一列,因为日期已经包含在文件名中,用户单击文件名打开文件。最坏的情况下,这也行。谢谢@您可以包括另一列作为数据源的一部分,而不显示它。只需使用BoundFields并关闭网格上的AutoGenerateColumns。@Albert如果不想看到该列,则应在网格视图中创建并设置AutoGenerateColumns=“false”。(请参见编辑)。
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