vb.net dropdownlist populate

2019-08-26 18:18发布

i need to populate my dropdownlist with month and year values to getdate + 1 year. I also want them in text format so "January 2009 and not 1 2009. This is the code i have written to populate the values. how do i convert them to January from 1?

Public Sub Load_xxxx(ByRef DDL As System.Web.UI.WebControls.DropDownList) Try Dim i As Integer Dim j As Integer For i = Now.Year To Now.Year For j = Now.Month To Now.Month + 11 DDL.Items.Add((j.ToString) + " " + (i.ToString)) Next Next Catch ex As Exception ReportError(ex) End Try End Sub

3条回答
家丑人穷心不美
2楼-- · 2019-08-26 18:28
Dim time As DateTime = DateTime.Now
DDL.Items.Clear()
While time <= time.AddYears(1)
    DDL.Items.Add((time.AddMonths(j)).ToString("MMMM yyyy"))
    time = time.AddMonths(1)
Next
查看更多
The star\"
3楼-- · 2019-08-26 18:30

If your just trying to convert the integer value to month name an easy way to do it would be just to create an array of strings containing the month names and use your j value as the index.

查看更多
别忘想泡老子
4楼-- · 2019-08-26 18:33

How about

Dim time As DateTime = DateTime.Now
    Dim j As Integer
    For j = 0 To 11
        DDL.Items.Add((time.AddMonths(j)).ToString("MMMM"))
    Next
查看更多
登录 后发表回答