开发者

vb.net dropdownlist populate

开发者 https://www.devze.com 2022-12-16 00:47 出处:网络
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.

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 populat开发者_如何学Ce 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


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


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消