我需要按照我的asp.net页面上显示的日期(月份和年份)对记录进行排序;
任何想法/建议都会有所帮助。
这是我目前的代码
<table width="40%" border="0" style="margin-left:auto; margin-right:auto;">
<tr><td><asp:Label ID="lblGridHeader" CssClass="TextFont" Text="" runat="server"></asp:Label></td></tr>
<tr>
<td align="center">
<asp:GridView ID="gvInvoiceList" runat="server" AutoGenerateColumns="false" AllowSorting="true">
<columns>
<asp:TemplateField ItemStyle-Width="10%" HeaderText="File Type">
<ItemTemplate><asp:HyperLink ID="imgFileType" ImageUrl="images/Icon_Pdf.gif" NavigateUrl='<%# SetNavigateUrl(Eval("Name")) %>' runat="server"></asp:HyperLink></ItemTemplate>
</asp:TemplateField>
<asp:boundfield datafield="Name" headertext="Invoice #"/>
<asp:boundfield datafield="LastWriteTime" headertext="Date Modified"/>
</columns>
</asp:GridView>
</td>
</tr>
</table>
代码背后:
If files.Count > 0 Then
Dim DT As New DataTable()
DT.Columns.Add(New DataColumn("Name", System.Type.GetType("System.String")))
DT.Columns.Add(New DataColumn("LastWriteTime", System.Type.GetType("System.String")))
Dim strCurrentMonth As String = ""
For Each f As FileInfo In files
If (MonthName(f.LastWriteTime.Month) <> strCurrentMonth) And (strCurrentMonth <> "") Then
gvInvoiceList.DataSource = DT
gvInvoiceList.DataBind()
lblGridHeader.Text = MonthName(f.LastWriteTime.Month) & " - " & Year(f.LastWriteTime)
Else
lblGridHeader.Text = MonthName(f.LastWriteTime.Month) & " - " & Year(f.LastWriteTime)
End If
Dim Row1 As DataRow
Row1 = DT.NewRow()
Row1("Name") = f.Name
Row1("LastWriteTime") = f.LastWriteTime
DT.Rows.Add(Row1)
strCurrentMonth = MonthName(f.LastWriteTime.Month)
Next
gvInvoiceList.DataSource = DT
gvInvoiceList.DataBind()
Else
lblSummary.Text = "No data to show."
End If
答案 0 :(得分:3)
您可以使用Repeater执行此操作。 喜欢这个(你应该能够很容易地适应它):
<asp:Repeater ID="rpt" runat="server" OnItemDataBound="rpt_RowDataBound">
<ItemTemplate>
<table runat="server" style="color: White; background-color: #3A4F63;" visible="false"
id="headerTable">
<tr>
<td colspan="3">
<asp:Label ID="headerTitle" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td style="width: 200px; background-color: #3A4F63; color: White;">
Name
</td>
<td style="width: 200px;">
Directory Name
</td>
<td style="width: 200px;">
Creation Time
</td>
</tr>
</table>
<!-- These are the actual data items -->
<!-- Bind to your specific properties i.e. Invoice #, file type, etc. -->
<table>
<tr>
<td style="width: 200px;">
<asp:Label ID="lblName" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
</td>
<td style="width: 200px;">
<asp:Label ID="lblDirName" runat="server" Text='<%#Eval("DirectoryName") %>'></asp:Label>
</td>
<td style="width: 200px;">
<asp:Label ID="lblCreationTime" runat="server" Text='<%#Eval("CreationTime") %>'></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
在Code Behind上,OnItemDataBound
看起来像这样:
Private month As Integer = -1
Private year As Integer = -1
Protected Sub rpt_RowDataBound(sender As Object, e As RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
'Binding to FileInfo objects. You are binding to DataTable. Adjust it accordingly
If month <> TryCast(e.Item.DataItem, FileInfo).CreationTime.Month OrElse year <> TryCast(e.Item.DataItem, FileInfo).CreationTime.Year Then
month = TryCast(e.Item.DataItem, FileInfo).CreationTime.Month
year = TryCast(e.Item.DataItem, FileInfo).CreationTime.Year
e.Item.FindControl("headerTable").Visible = True
TryCast(e.Item.FindControl("headerTitle"), Label).Text = "Files for " & TryCast(e.Item.DataItem, FileInfo).CreationTime.ToShortDateString()
Else
e.Item.FindControl("headerTable").Visible = False
End If
End If
End Sub
我将数据绑定到转发器的方式是这样的:
Dim fi As FileInfo() = New DirectoryInfo("C:\").GetFiles().OrderByDescending(Function(x) x.CreationTime).ToArray()
rpt.DataSource = fi
rpt.DataBind()
生成此输出:
答案 1 :(得分:0)
您拥有的ASP.NET内容应放在Repeater
中。然后,您拥有的files
应按月份和日期进行分组。所以基本上你最终会得到一个父子列表。作为文件组的父级将绑定到Repeater
,属于该组的文件的子级将绑定到GridView
中的Repeater
。
ASP.NET内容将是这一行。请注意gvInvoiceList
DataSource
属性绑定到InvoiceList
,这是我提出的组的属性(您将在下面的代码中看到),它将包含一个文件列表属于该组。
<asp:Repeater ID="repInvoiceGroups" runat="server">
<ItemTemplate>
<table width="40%" border="0" style="margin-left:auto; margin-right:auto;">
<tr><td><asp:Label ID="lblGridHeader" CssClass="TextFont"
Text='<%# Eval("MonthYear", "{0:MMMM yyyy}") %>'
runat="server"></asp:Label></td></tr>
<tr>
<td align="center">
<asp:GridView ID="gvInvoiceList" runat="server"
AutoGenerateColumns="false" AllowSorting="true"
DataSource='<%# Eval("InvoiceList") %>'>
<columns>
<asp:TemplateField ItemStyle-Width="10%" HeaderText="File Type">
<ItemTemplate><asp:HyperLink ID="imgFileType" ImageUrl="images/Icon_Pdf.gif" NavigateUrl='<%# SetNavigateUrl(Eval("Name")) %>' runat="server"></asp:HyperLink></ItemTemplate>
</asp:TemplateField>
<asp:boundfield datafield="Name" headertext="Invoice #"/>
<asp:boundfield datafield="LastWriteTime" headertext="Date Modified"/>
</columns>
</asp:GridView>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
至于代码,我不能流利地使用DataTable
来获得我用于答案的ASP.NET结构所需的父子关系,但是使用普通类应该很容易实现。而且我也不熟练使用VB.NET,所以请原谅我将使用C#的例子,我想你应该能够很容易地将它转换成VB.NET。
我正在使用Linq
进行分组,并使用匿名类来建立父子关系,因此代码相当短。
repInvoiceGroups.DataSource = files
.GroupBy(f => f.LastWriteTime.ToString("yyyy-MM"))
.Select(g => new {
MonthYear = DateTime.ParseExact(g.Key, "yyyy-MM", CultureInfo.InvariantCulture),
InvoiceList = g.OrderByDescending(f => f.LastWriteTime) })
.OrderByDescending(o => o.MonthYear);
repInvoiceGroups.DataBind();
p / s:代码是在文本编辑器中编写的,未经测试。如果您遇到任何错误,请告诉我。 :)