在asp.net中显示按月和年分组的记录 [英] Displaying records grouped by month and year in asp.net
本文介绍了在asp.net中显示按月和年分组的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要按 asp.net 页面上显示的日期(月份和年份)对我的记录进行排序;
任何想法/建议都会有所帮助.
I need to sort my records by date (month & year) as displayed on my asp.net page;
Any ideas / suggestions would be helpful.
这是我目前拥有的代码
<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
推荐答案
您可以使用中继器执行此操作.类似这样的东西(你应该很容易适应):
You can do this with a Repeater. Something like this (you should be able to adapt it easily):
<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
看起来像这样:
On Code Behind, the OnItemDataBound
looks like this:
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
我将数据绑定到转发器的方式是这样的:
The way I bound my data to the repeater is like this:
Dim fi As FileInfo() = New DirectoryInfo("C:").GetFiles().OrderByDescending(Function(x) x.CreationTime).ToArray()
rpt.DataSource = fi
rpt.DataBind()
产生这个输出:
这篇关于在asp.net中显示按月和年分组的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文