按月份和年份asp.net分组显示记录 [英] Displaying records grouped by month and year in asp.net

查看:218
本文介绍了按月份和年份asp.net分组显示记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要按日期我的记录(月&安培;年)整理我的asp.net页面上显示;结果
任何意见/建议将是有益的。

这是code我目前有

 <表格的宽度=40%BORDER =0的风格=保证金左:自动;保证金右:自动;>
                    < TR>< TD>< ASP:标签ID =lblGridHeader的CssClass =TEXTFONT文本==服务器>< / ASP:标签>< / TD>< / TR&GT ;
                    &所述; TR>
                        < TD ALIGN =中心>
                            < ASP:GridView控件ID =gvInvoiceList=服务器的AutoGenerateColumns =false的AllowSorting =真>
                                <列>
                                    < ASP:的TemplateField ItemStyle-WIDTH =10%的HeaderText =文件类型>
                                        <&ItemTemplate中GT;< ASP:超链接ID =imgFileType的ImageUrl =图片/ Icon_Pdf.gifNavigateUrl ='<%#SetNavigateUrl(EVAL(姓名))%GT;' =服务器>< / ASP:超链接>< / ItemTemplate中>
                                    < / ASP:的TemplateField>
                                  < ASP:BoundField的数据字段=名称HEADERTEXT =#发票/>
                                  < ASP:BoundField的数据字段=LastWriteTimeHEADERTEXT =修改日期/>
                                < /列>
                            < / ASP:GridView的>
                        < / TD>
                    < / TR>
                < /表>

后面的 code:

 如果files.Count> 0,则
        昏暗DT作为新的DataTable()
        DT.Columns.Add(新的DataColumn(名,System.Type.GetType(System.String)))
        DT.Columns.Add(新的DataColumn(LastWriteTime,System.Type.GetType(System.String)))        昏暗strCurrentMonth作为字符串=        对于每个F作为的FileInfo在文件列表中
            如果(MONTHNAME(f.LastWriteTime.Month)LT;> strCurrentMonth)和(strCurrentMonth<>中),然后
                gvInvoiceList.DataSource = DT
                gvInvoiceList.DataBind()                lblGridHeader.Text = MONTHNAME(f.LastWriteTime.Month)及 - 与&年(f.LastWriteTime)
            其他
                lblGridHeader.Text = MONTHNAME(f.LastWriteTime.Month)及 - 与&年(f.LastWriteTime)
            万一            昏暗ROW1作为的DataRow
            ROW1 = DT.NewRow()
            ROW1(姓名)= f.Name
            ROW1(LastWriteTime)= f.LastWriteTime
            DT.Rows.Add(ROW1)            strCurrentMonth = MONTHNAME(f.LastWriteTime.Month)
        下一个
        gvInvoiceList.DataSource = DT
        gvInvoiceList.DataBind()
    其他
        lblSummary.Text =没有数据显示。
    万一


解决方案

您可以使用中继器做到这一点。事情的的这个(你应该能够很容易地适应它):

 < ASP:直放站ID =RPT=服务器OnItemDataBound =rpt_RowDataBound>
        <&ItemTemplate中GT;
            <表=服务器的风格=颜色:白色,背景色:#3A4F63;可见=假
                ID =headerTable>
                &所述; TR>
                    &所述; TD列跨度=3>
                        < ASP:标签ID =headerTitle=服务器>< / ASP:标签>
                    < / TD>
                < / TR>
                &所述; TR>
                    < TD风格=宽度:200像素;背景颜色:#3A4F63;颜色:白色;>
                        名称
                    < / TD>
                    < TD风格=宽度:200像素;>
                        目录名称
                    < / TD>
                    < TD风格=宽度:200像素;>
                        创建时间
                    < / TD>
                < / TR>
            < /表>
            &所述;! - 这些是实际的数据项 - >
            <! - 绑定到特定的属性,即发票#,文件类型等 - >
            <表>
                &所述; TR>
                    < TD风格=宽度:200像素;>
                        < ASP:标签ID =lblName=服务器文本='<%#的eval(姓名)%>'>< / ASP:标签>
                    < / TD>
                    < TD风格=宽度:200像素;>
                        < ASP:标签ID =lblDirName=服务器文本='<%#的eval(目录名)%>'>< / ASP:标签>
                    < / TD>
                    < TD风格=宽度:200像素;>
                        < ASP:标签ID =lblCreationTime=服务器文本='<%#的eval(CREATIONTIME)%>'>< / ASP:标签>
                    < / TD>
                < / TR>
            < /表>
        < / ItemTemplate中>
    < / ASP:直放站>

在code背后, OnItemDataBound 是这样的:

 私人月份为整数= -1
私人今年为整数= -1
保护小组rpt_RowDataBound(发送者为对象,E为RepeaterItemEventArgs)    如果e.Item.ItemType = ListItemType.Item OrElse运算e.Item.ItemType = ListItemType.AlternatingItem然后
    绑定到的FileInfo对象。要绑定到数据表。相应调整
        如果一个月<> TryCast(e.Item.DataItem,FileInfo的).CreationTime.Month OrElse运算年<> TryCast(e.Item.DataItem,FileInfo的).CreationTime.Year然后
            月= TryCast(e.Item.DataItem,FileInfo的).CreationTime.Month
            年= TryCast(e.Item.DataItem,FileInfo的).CreationTime.Year
            e.Item.FindControl(headerTable)。可见=真
            TryCast(e.Item.FindControl(headerTitle),标签)。文本=文件为&放大器; TryCast(e.Item.DataItem,FileInfo的).CreationTime.ToShortDateString()
        其他
            e.Item.FindControl(headerTable)。可见=假
        万一
    万一
结束小组

我绑定我的数据中继的方式是这样的:

 昏暗网络上网的FileInfo()=新的DirectoryInfo(C:\\)。的GetFiles()OrderByDescending(功能(X)x.CreationTime).ToArray()
rpt.DataSource =网络
rpt.DataBind()

产生这样的输出:

I need to sort my records by date (month & year) as displayed on my asp.net page;
Any ideas / suggestions would be helpful.

This is the code I currently have

                <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>

Code behind:

    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>

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()

Produces this output:

这篇关于按月份和年份asp.net分组显示记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆