c# - 在下拉列表中设置从 2012/11/01 到 2012 年 11 月的日期格式 C#

标签 c#

大家好,我需要 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/

相关文章:

c# - 表格末尾的 HTML Agility Pack tr 插入

c# - Word 加载项 : Getting user credentials which were used to open the document from SharePoint

c# - 用于创建数独板的蛮力算法

c# - 我可以让这个 Linq to EF 更有效率吗?

c# - 在 WinRT 应用程序中即时本地化

c# - 如何比较两个对象数组

c# - Sqlite数据库中的加密和解密?

c# - 如何在以编程方式创建的文本框中设置焦点?

c# - 重构switch语句

c# - 套接字无响应