大家好,我需要 C# 方面的帮助,如何使用 DataTextFormatingString 格式化带有日期的 dataTextField。
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, " ");
最佳答案
您只需设置格式即可:
monstart.DataTextFormatString = "{0:MMMM yyyy}";
解决我们关于实际上是否允许自定义格式的问题。该代码调用 DataBinder.GetPropertyValue
,并使用通用 string.Format
进行格式化:
// .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);
}
这表明自定义格式将在此范围内运行。
关于c# - 在下拉列表中设置从 2012/11/01 到 2012 年 11 月的日期格式 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20097630/