我有一个请求,要求显示一个类似于日历和行月份列日历的网格。
像这样的东西
Month |Sun|Mon|Tue|Wed|Thu|Fri|Sat|Sun|Mon|Tue|Wed|Thu|Fri|Sat|Sun|Mon|Tue|Wed|...|Thu|
January | 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11|...................| 31|
Febrary | | | | 1| 2| 3| 4| 5| 6| 7|...........................| 29|
...
December| 30| 31| | | | | 1| 2| 3| 4|............................
有没有办法做到这一点,考虑到一年中的开始日必须是真实的日期名称,而不是常量,这必须动态生成
我试试这个,但我的想法开始了!
Private Sub GenerateCalendar(ByVal year As Integer)
Dim colsCount As Integer = 3
If Bisiesto(año) Then
colsCount = colsCount + 366
Else
colsCount = colsCount + 365
End If
Dim dtCalendar As New DataTable
dtCalendar.Columns.Add("Mes")
Dim dayMonthYear As Date = New Date(year , 1, 1)
'Showing the distribution of the first month
While dayMonthYear.Year = año And dayMonthYear.Month = 1
Dim dtColumn As DataColumn = New DataColumn()
dtColumn.ColumnName = WeekdayName(Weekday(dayMonthYear)) & dayMonthYear.Day & dayMonthYear.Month
dtColumn.Caption = WeekdayName(Weekday(dayMonthYear))
dtCalendar.Columns.Add(dtColumn)
dayMonthYear = diaMesAño.AddDays(1)
End While
Dim row As DataRow = dtCalendario.NewRow()
'Here I need to distribute the days on its column day
dtCalendario.Rows.Add(row)
wdgCalendario.DataSource = dtCalendar
wdgCalendario.DataBind()
End Sub
Public Function LeapYear(ByVal year As Integer)
Dim isLeapYear As Boolean = False
If year Mod 4 = 0 Then
If (year Mod 100 = 0) And Not (year Mod 400 = 0) Then
isLeapYear = False
Else
isLeapYear = True
End If
Else
isLeapYear = False
End If
Return isLeapYear
End Function
答案 0 :(得分:2)
注意:您不能在DataTable中拥有重复的列名,因此我在工作日中添加了一周中的星期数。但是请亲自看看:
Private Function GetCalendarTable(year As Int32) As DataTable
Dim curCulture = System.Globalization.CultureInfo.CurrentCulture
Dim firstYearDate = New Date(year, 1, 1)
Dim currentDate = firstYearDate
Dim tblCalendar = New DataTable
tblCalendar.Columns.Add(New DataColumn("MonthName"))
Dim maxDiff = 0
For m = 1 To 12
'find max difference between first year's weekday and month's first weekday
'if the latter is earlier in the week, it is considered to be in the next week
Dim monthFirstWeekDay = New Date(year, m, 1).DayOfWeek
Dim diff = (7 + (monthFirstWeekDay - firstYearDate.DayOfWeek)) Mod 7
If diff > maxDiff Then
maxDiff = diff
End If
Next
Dim weekDayNum = curCulture.Calendar.GetDaysInMonth(year, 1) + maxDiff
' Create DataColumns with weekday as ColumnsName
For wd = 1 To weekDayNum
Dim weekday = currentDate.ToString("ddd")
Dim weekInMonth = (From col In tblCalendar.Columns.Cast(Of DataColumn)()
Where col.ColumnName Like String.Format("{0} W#", weekday)).Count + 1
Dim columnName = String.Format("{0} W{1}", weekday, weekInMonth)
tblCalendar.Columns.Add(New DataColumn(columnName))
currentDate = currentDate.AddDays(1)
Next
' Create the DataRows(every month)
For m = 1 To 12
Dim daysInMonth = curCulture.Calendar.GetDaysInMonth(year, m)
Dim firstMonthDate = New Date(year, m, 1)
Dim daysBefore = (7 + (firstMonthDate.DayOfWeek - firstYearDate.DayOfWeek)) Mod 7
Dim daysBehind = tblCalendar.Columns.Count - (daysBefore + daysInMonth) - 1
Dim monthDays = From d In Enumerable.Range(1, daysInMonth)
Select New With {.Day = d.ToString}
Dim emptyDaysBefore = From d In Enumerable.Range(1, daysBefore)
Select New With {.Day = ""}
Dim emptyDaysAfter = From d In Enumerable.Range(1, daysBehind)
Select New With {.Day = ""}
Dim monthName = curCulture.DateTimeFormat.GetMonthName(m)
' piece together parts
Dim allFields = ({New With {.Day = monthName}}.
Union(emptyDaysBefore).
Union(monthDays).
Union(emptyDaysAfter).
Select(Function(d) d.Day)).ToArray
tblCalendar.Rows.Add(allFields)
Next
Return tblCalendar
End Function