Home > Net >  Generate days of the week in datagridview in vb.net
Generate days of the week in datagridview in vb.net

Time:02-16

I am trying to get the days of the week in the header of my datagridview depending on the selected period of the combobox

enter image description here

So far I have achieved the following to establish the days of the week but I have noticed that I always get 31.

In February we only have 28 days in which part of the following code am I failing

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim dias_de_la_semana As New ArrayList From {"LUN", "MAR", "MIE", "JUE", "VIEW", "SAB", "DOM"}
    Dim MiFecha As Date = Now ' o toda otra fecha
    Dim Limite As Integer = 31  ' Puestas el valor que quieres
    For i As Integer = 1 To Limite Step 7
        For j = 0 To 7
            If i   j <= Limite Then
                dgvdias.Columns.Add(0, dias_de_la_semana(MiFecha.DayOfWeek) & "-" & i   j)
            Else
                i = Limite
                Exit For
            End If
            MiFecha = MiFecha.AddDays(1)
        Next
    Next
End Sub

CodePudding user response:

Try changing your line

 Dim Limite As Integer = 31

into

Dim Limite As Integer = System.DateTime.DaysInMonth(MiFecha.Year, MiFecha.Month)

CodePudding user response:

February can have 29 days in leap years. So you have to account for the current year to be accurate. Have a look here to see how you could easily retrieve the number of days for the current month.

Also, it's not recommended to populate a DGV manually, I suggest using a datatable instead, and use it as data source for your DGV. Probably you will want to do more and populate the grid with user input to save data to a DB.

A simplistic example:

dim current_year as integer = 2020
dim current_month as integer = 2
Dim days_in_month As Integer = System.DateTime.DaysInMonth(current_year, current_month)

System.Console.WriteLine("Days in month: " & days_in_month)

' start at 1st day of current month
dim d as new DateTime(current_year, current_month, 1)
' offset -1 day for the loop
d = d.AddDays(-1)

for i as integer = 1 to days_in_month
    ' date1 = new DateTime(2008, 5, 1, 8, 30, 52);
    d = d.AddDays(1)
    System.Console.WriteLine("Day: " & i & " - date: " & d.ToString("dddd, MMMM dd"))
next

Output:

Days in month: 29
Day: 1 - date: Saturday, February 01
Day: 2 - date: Sunday, February 02
Day: 3 - date: Monday, February 03
Day: 4 - date: Tuesday, February 04
Day: 5 - date: Wednesday, February 05
Day: 6 - date: Thursday, February 06
Day: 7 - date: Friday, February 07
Day: 8 - date: Saturday, February 08
Day: 9 - date: Sunday, February 09
Day: 10 - date: Monday, February 10
Day: 11 - date: Tuesday, February 11
Day: 12 - date: Wednesday, February 12
Day: 13 - date: Thursday, February 13
Day: 14 - date: Friday, February 14
Day: 15 - date: Saturday, February 15
Day: 16 - date: Sunday, February 16
Day: 17 - date: Monday, February 17
Day: 18 - date: Tuesday, February 18
Day: 19 - date: Wednesday, February 19
Day: 20 - date: Thursday, February 20
Day: 21 - date: Friday, February 21
Day: 22 - date: Saturday, February 22
Day: 23 - date: Sunday, February 23
Day: 24 - date: Monday, February 24
Day: 25 - date: Tuesday, February 25
Day: 26 - date: Wednesday, February 26
Day: 27 - date: Thursday, February 27
Day: 28 - date: Friday, February 28
Day: 29 - date: Saturday, February 29

You can adjust date formatting according to your needs.

  • Related