Reputation: 31
Hi guys i need help with C#, How do i format a dataTextField with a date using DataTextFormatingString.
monstart.DataSource = dt;
monstart.DataTextField = "Period";//This return 2012/11/01, and i want to display it as November 2012
monstart.DataValueField = "SalaryMonthYear";
//monstart.DataTextFormatString = "";
monstart.DataBind();
monstart.Items.Insert(0, " ");
Upvotes: 2
Views: 157
Reputation: 67898
You can just set the format:
monstart.DataTextFormatString = "{0:MMMM yyyy}";
To address our question on whether or not custom formats are in fact allowed. The code calls DataBinder.GetPropertyValue
, and it uses the general string.Format
to do the formatting:
// .NET DataBinder class
public static string GetPropertyValue(object container, string propName,
string format)
{
object propertyValue = DataBinder.GetPropertyValue(container, propName);
if (propertyValue == null || propertyValue == DBNull.Value)
{
return string.Empty;
}
if (string.IsNullOrEmpty(format))
{
return propertyValue.ToString();
}
return string.Format(format, propertyValue);
}
thus indicating that custom formats will operate in this scope.
Upvotes: 5
Reputation: 11287
You can use this format:
monstart.DataTextFormatString = "MMMM dd. yyyy"
Upvotes: 1