bind Day (1-31) to dropdown programmatically and 0's to day < 10?
I have the current method in a helper file to bi开发者_运维百科nd monthly days to a dropdown and I wonder how I should modify it to add a leading 0 to 1-9?
public DropDownList PopulateDay(DropDownList ddlControlDay)
{
for (int i = 0; i <= 31; i = i + 1)
ddlControlDay.Items.Add(i.ToString("D2"));
return ddlControlDay;
}
I could convert to string, check length/modify and parse to int but is there a better way?
Thanks in advance.
The format "D02"
will pad with zeroes up to the limit. See Custom Numeric Format Strings.
Using LINQ, it might be:
blah.DataSource = Enumerable.Range(1,30).Select(x => x.ToString("D02"));
// or with Items.AddRange, perhaps
Happy coding.
And please fix the formatting of your code and/or use braces on the loop. I always use braces because then there is no "fake dangling code" like in the post :-)
精彩评论