列出一个月的所有日期ASP.NET C#

时间:2015-07-30 09:51:11

标签: c# asp.net date gridview

我想生成一个包含一个月所有日期的数据透视表,如下图所示。

enter image description here

有没有办法在sql或c#中动态制作日期列表?月份和年份绑定到下拉列表,手动填充。

   <asp:Label ID="LlbMes" runat="server" Text="Mês"></asp:Label>
  </td>
    <td> <%--Months--%>
    <asp:DropDownList ID="DDMes" runat="server" Height="18px" Width="90px">
   <asp:ListItem Text="Janeiro" Value="1"></asp:ListItem> 
   <asp:ListItem Text="Fevereiro" Value="2"></asp:ListItem>
   <asp:ListItem Text="Março" Value="3"></asp:ListItem> 
   <asp:ListItem Text="Abril" Value="4"></asp:ListItem> 
   <asp:ListItem Text="Maio" Value="5"></asp:ListItem>
   <asp:ListItem Text="Junho" Value="6"></asp:ListItem>
   <asp:ListItem Text="Julho" Value="7"></asp:ListItem>
   <asp:ListItem Text="Agosto" Value="8"></asp:ListItem>
   <asp:ListItem Text="Setembro" Value="9"></asp:ListItem>
   <asp:ListItem Text="Outubro" Value="10"></asp:ListItem>
   <asp:ListItem Text="Novembro" Value="11"></asp:ListItem>
   <asp:ListItem Text="Dezembro" Value="12"></asp:ListItem>
    </asp:DropDownList>&nbsp;&nbsp;
    </td>
    <td>
      <asp:Label ID="LlbAno" runat="server" Text="Ano"></asp:Label>
    </td>
    <td><%-- Years--%>
        <asp:DropDownList ID="DDAno" runat="server" Height="18px" Width="90px">
         <asp:ListItem Value="2015">2015</asp:ListItem>
        <asp:ListItem Value="2014">2014</asp:ListItem>
        <asp:ListItem Value="2013">2013</asp:ListItem>
        <asp:ListItem Value="2012">2012</asp:ListItem>
        <asp:ListItem Value="2011">2011</asp:ListItem>
        <asp:ListItem Value="2010">2010</asp:ListItem>
        <asp:ListItem Value="2009">2009</asp:ListItem>
        <asp:ListItem Value="2008">2008</asp:ListItem>
        <asp:ListItem Value="2007">2007</asp:ListItem>
        <asp:ListItem Value="2006">2006</asp:ListItem>
        <asp:ListItem Value="2005">2005</asp:ListItem>
        <asp:ListItem Value="2004">2004</asp:ListItem>
        <asp:ListItem Value="2003">2003</asp:ListItem>
        <asp:ListItem Value="2002">2002</asp:ListItem>
        <asp:ListItem Value="2001">2001</asp:ListItem>
    </asp:DropDownList>

4 个答案:

答案 0 :(得分:2)

试试这个。谢谢K&amp; R的“For”循环

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication38
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime firstOfMonth = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
            DateTime lastDayOfMonth = firstOfMonth.AddMonths(1).AddDays(-1);

            List<DateTime> daysOfMonth = new List<DateTime>();
            for (DateTime dayCounter = firstOfMonth; dayCounter <= lastDayOfMonth; dayCounter = dayCounter.AddDays(1))
            {
                daysOfMonth.Add(dayCounter);
            }

        }
    }
}

答案 1 :(得分:1)

 List<DateTime> daysOfMonth = Enumerable.Range(1, DateTime.DaysInMonth(year, month))  // Days: 1, 2 ... 31 etc.
                             .Select(day => new DateTime(year, month, day)) // Map each day to a date
                             .ToList(); 

以上返回特定年份中特定月份的天数

答案 2 :(得分:1)

int year=2015;
int month=2;
IEnumerable<int> daysInMonths = Enumerable.Range(1, DateTime.DaysInMonth(year, month));

答案 3 :(得分:0)

 int days = DateTime.DaysInMonth(yearnum, monthnum);


            for (int dayCounter = 1; dayCounter <= days; dayCounter++)
            {
                string day_of_week = (Convert.ToDateTime("" + year + "-" + month + "-" + dayCounter + "").DayOfWeek).ToString();
                sb.Append(@"<td  style='text-align:center;background-color:#2cbed8;width:20px;'><b>" + dayCounter + " " + day_of_week + "</b></td>");
            }

其中: 年度和年龄monthnum必须在“int”中例如: - DateTime.DaysInMonth(2016,2)
结果是
enter image description here

相关问题