一张Aritcles表里,有一个PostTime字段,里面是记录的是DATETIME.NOW,那么,怎么实现如下的效果:
2008年十月
文章1
文章2
文章3
2008年九月
文章1
文章2
文章3
.....
2007年...
.....

解决方案 »

  1.   

    其实DateTime列也可以当成字符串列来处理。
      

  2.   

    select left(convert(char(8),PostTime,112),6) as PostTime,Title
    from Aritcles
    group by left(convert(char(8),PostTime,112),6)
    order by 1 desc
      

  3.   

    假设日期在一个GridView的第三个列显示。且之前已经按日期字段排序好了。那么可以override Render.
    protected override void Render(HtmlTextWriter writer)
    {
            DateTime oldDate = new DateTime(1990, 1, 1);        Table table = (Table)GridView1.Controls[0];
            foreach (GridViewRow row in GridView1.Rows)
            {
                DateTime newDate = Convert.ToDateTime(row.Cells[2].Text); //假定日期字段在GridView的第3列显示
                if (newDate.Year != oldDate.Year || newDate.Month != oldDate.Month) // 如果两个日期的年、月不相同
                {
                    int index = table.Rows.GetRowIndex(row);                GridViewRow insertRow = new GridViewRow(index, index, DataControlRowType.DataRow, DataControlRowState.Normal);
                    TableCell cell = new TableCell();
                    cell.Text = newDate.Year + "年" + newDate.Month + "月";
                    cell.ColumnSpan = GridView1.Columns.Count;
                    insertRow.Cells.Add(cell);
                    table.Controls.AddAt(index, insertRow);                oldDate = newDate;
                }
            }
            base.Render(writer);
    }
      

  4.   

    SELECT DATEPART(year, PostTime) as y,DATEPART(month, PostTime) as m,title from Articles
    group by y,m
    order by PostTime
      

  5.   

    没环境测试,请参考SELECT DATEPART(year, PostTime) as y,DATEPART(month, PostTime) as m,title from Articles 
    group by y,m 
    order by PostTime 
      

  6.   

    一张Aritcles表里,有一个PostTime字段,里面是记录的是DATETIME.NOW,文章标题的字段是ArticleCaption 
    那么,怎么实现如下的效果: 2008年十月 
    文章的标题 
    文章的标题 
    文章的标题 
    2008年九月 
    文章的标题 
    文章的标题 
    文章的标题 
    ..... 
    2007年... 
    .....
      

  7.   

    试都没试又问!真得让人手把手吗? 直接拷进去,那个连接字符串可没办法手把手了。
    .aspx<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                DataKeyNames="uid" DataSourceID="SqlDataSource1" EmptyDataText="没有可显示的数据记录。" 
                onprerender="GridView1_PreRender">
                <Columns>                               
                    <asp:BoundField DataField="ArticleCaption" HeaderText="标题" SortExpression="ArticleCaption" />
                    <asp:BoundField DataField="PostTime" HeaderText="日期" 
                        SortExpression="PostTime" />
                </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:数据库连接字符串%>" 
                ProviderName="<%$ ConnectionStrings:数据库连接字符串.ProviderName %>" 
                SelectCommand="SELECT ArticleCaption, PostTime FROM [Aritcles] order by PostTime" 
                />.cs
    protected override void Render(HtmlTextWriter writer)
    {
            DateTime oldDate = new DateTime(1990, 1, 1);        Table table = (Table)GridView1.Controls[0];
            foreach (GridViewRow row in GridView1.Rows)
            {
                DateTime newDate = Convert.ToDateTime(row.Cells[1].Text); //假定日期字段在GridView的第2列显示
                if (newDate.Year != oldDate.Year || newDate.Month != oldDate.Month) // 如果两个日期的年、月不相同
                {
                    int index = table.Rows.GetRowIndex(row);                GridViewRow insertRow = new GridViewRow(index, index, DataControlRowType.DataRow, DataControlRowState.Normal);
                    TableCell cell = new TableCell();
                    cell.Text = newDate.Year + "年" + newDate.Month + "月";
                    cell.ColumnSpan = GridView1.Columns.Count;
                    insertRow.Cells.Add(cell);
                    table.Controls.AddAt(index, insertRow);                oldDate = newDate;
                }
            }
            base.Render(writer);
    }
      

  8.   

    重写render,参数4楼不然就搞个嵌套repeater