Automatic Binding of Days, Months, Years to a DropDownList Control



This blog shows a simple snippet of automatic binding of Days, Months, Years to a DropDownList/ListBox control.

Below following methods were used for the purpose.

For binding Days:
  1. System.DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month).
  • Method returns Total Days in a Month.

  • Method returns the last day of the month.
4.jpg

For binding DayNames
  1. Doing a for-each loop to retrieve Day Names from DateTimeFormatInfo.CurrentInfo.DayNames.
2.jpg

For binding MonthNames
  1. Doing a for-each to retrieve Month Names from DateTimeFormatInfo.CurrentInfo.MonthNames.
3.jpg

Some styling is also applied; you can uncomment those code and test the samples attached.

The output will shown as below.

image2.gif


Please rate and post your comments !
 

Thank You!

Up Next
    Ebook Download
    View all
    Learn
    View all