Reputation: 1
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
Upvotes: 0
Views: 627
Reputation: 6969
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
Upvotes: 0