简体   繁体   中英

How to loop date in datagridview in vb.net

I am new to vb and I have a computebtn, numberofyear.text and a datetimepicker.

If I click the button compute, I want to loop the month. For example, if put 1 year (so twelve loops) the date from 8/1/2015 to 9/1/2016 will will display in datagrid view.

This is my code so far.

Dim currentDate as DateTime = me.timepicker.Value
Dim endDate = beginDate.AddYears(cint(me.textbox1.text))
Dim monthCount as integer = 0

While currentDate.ticks <= endDate.ticks

    monthCount += 1
    Me.DataGridView1.Rows.Add(monthCount, currentDate.ToString("MM/dd/yy"))
    currentDate = currentDate.AddMonths(1)
Next

You can first find the month count by using DateDiff function. Then loop and use the AddMonths method to add each month to the gird.

    Dim currentDate As DateTime = me.timepicker.Value
    Dim endDate As DateTime = currentDate.AddYears(CInt(Me.TextBox1.Text))
    Dim monthCount As Integer = CInt(DateDiff(DateInterval.Month, currentDate, endDate))
    For i = 0 To monthCount
        DataGridView1.Rows.Add(monthCount, currentDate.AddMonths(i).ToString("MM/dd/yy"))
    Next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM