【发布时间】:2011-10-11 11:17:12
【问题描述】:
我需要按照我的 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
【问题讨论】:
-
包括有关记录如何分组的一些详细信息。在不知道数据是什么样子的情况下,很难将您的需求可视化。
-
我不明白你为什么需要一个循环。如果数据库正在为您进行分组,您不能只显示查询结果吗?
-
我需要将它们显示在单独的块中,标题为“月年”
-
如果我是正确的,这可能需要客户端上的
<span>标记,然后动态创建gridview 的实例。