So i figured it's worth a little blog post.
The magic happens by using the DataTextFormatString property of the dropdownlist.
But i didn't know how to format the date properly until i figured out i had to use {0}.
Here is how to do it:
// Create a list with dummy dates
List<DateTime> dates = new List<DateTime>();
for (double i = 1; i <= 31; i++)
{
dates.Add(DateTime.Now.AddDays(i));
}
// Specify the dateformat (e.g. outputs today as "31-08-2009 18:40")
ddlMyDropdown.DataTextFormatString = "{0:dd-MM-yyyy hh:mm}";
// Databinding
ddlMyDropdown.DataSource = dates;
ddlMyDropdown.DataBind();
Note that .NET uses the culture of the current thread to format the date.
So if i used format {0:dd MMMM yyyy} which would output as "31 August 2009" and i want to write it out in another culture, let's say dutch (31 augustus 2009).
Then i need to execute the following code before i perform the databinding:
// Set current culture for current thread to dutch
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("nl-NL");
Hope this saves you some time googling for it ;)
/Ruud
No comments:
Post a Comment