我需要按我的 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
您可以使用中继器来完成此操作。某物like这个(你应该能够轻松适应它):
<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>
关于代码隐藏,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()
产生以下输出:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)