user2582729
user2582729

Reputation: 31

setting a date format in a dropdownlist from 2012/11/01 to November 2012 format C#

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

Answers (3)

Ashok
Ashok

Reputation: 1906

Try this one.

monstart.DataTextFormatString = "{MMMM yyyy}";

Upvotes: 0

Mike Perrenoud
Mike Perrenoud

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

Jens Kloster
Jens Kloster

Reputation: 11287

You can use this format:

   monstart.DataTextFormatString = "MMMM dd. yyyy"

Upvotes: 1

Related Questions