Reputation: 317
I want to generate a pivot table with all days of a month like this image below.
Is there a way to make the list of the days dynamically in sql or c#? The month and year are binded to a dropdownm, filled manually.
<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>
</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>
Upvotes: 2
Views: 3000
Reputation: 71
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>");
}
where:
yearnum & monthnum must be in "int"
Example:- DateTime.DaysInMonth(2016,2)
Result is
Upvotes: 0
Reputation: 368
int year=2015;
int month=2;
IEnumerable<int> daysInMonths = Enumerable.Range(1, DateTime.DaysInMonth(year, month));
Upvotes: 1
Reputation: 347
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();
Above returns the days of specific month in specific a year
Upvotes: 4
Reputation: 34421
Try this. thank you K&R for the "For" loop
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);
}
}
}
}
Upvotes: 2