In the database, I have a table ‘Years’, that contains columns for ‘YearId’, ‘YearStart’ and ‘YearEnd’ (don’t ask me why, its not my database).
I need to populate an asp:DropDownList with the different year values in the table, ie. I need the dropdown to be like:
//pseudocode
<select>
<option>2010</option>
<option>2011</option>
..etc..
</select>
But since the ‘YearStart’ and ‘YearEnd’ are stored in DateTime format, I’m getting this:
//pseudocode
<select>
<option>1/1/2010 12:00:00 AM</option>
<option>1/1/2011 12:00:00 AM</option>
..etc..
</select>
Here is how I am currently populating the DropDownList:
dropDownList.DataSource = DataContext.Years;
dropDownList.DataValueField = "YearId";
dropDownList.DataTextField = "YearStart";
dropDownList.DataBind();
Basically, I need the dropDownList.DataTextField to be in DateTime.Year form, ie:
//pseudocode
dropDownList.DataTextField = ("YearStart").Year;
So what would be the best way to populate the dropDownList and achieve what I need?
You can try including
DataTextFormatStringor
you can tweak the DataSource like this