Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Formating Data grid date column RSS

6 replies

Last post Apr 24, 2008 06:33 AM by codebased

(0)
  • codebased

    codebased

    Participant

    826 Points

    386 Posts

    Formating Data grid date column

    Apr 23, 2008 07:18 AM | LINK

    Hi there, I've a data grid with AutoColumn = false and I want to format my date column to MMDDYYYY on Itemsource changed.

     how can I do that?

     

    "If I've answered your query then please mark it as "Answered".
  • Jim Mangaly

    Jim Mangaly

    Contributor

    2646 Points

    383 Posts

    Re: Formating Data grid date column

    Apr 23, 2008 12:24 PM | LINK

    On the binding for the date column, use a converter that converts the input date to the desired date format.

    Jim (http://jimmangaly.blogspot.com/)

     

    http://www.identitymine.com/
  • deepty.302472

    deepty.302472

    Member

    78 Points

    91 Posts

    Re: Formating Data grid date column

    Apr 23, 2008 12:59 PM | LINK

    How do i can format the datetime to dd-MMM-YYYY of column if I am uisng the Datatempltecolumn.

    There is no option like converter in DatatemplateColumn.

    Thanks,

    T Sudarshan Reddy.

  • Jim Mangaly

    Jim Mangaly

    Contributor

    2646 Points

    383 Posts

    Re: Formating Data grid date column

    Apr 23, 2008 01:16 PM | LINK

    Check out this link in MSDN:  http://msdn2.microsoft.com/en-us/library/cc189753(VS.95).aspx

    Notice the following portion in the article where it defines a DataTemplateColumn. You can see that data binding is used to display text in the text block. Binding gives you an option to add a converter to modify the source to a format you want. See this article on converters:  http://msdn2.microsoft.com/en-us/library/cc278072(vs.95).aspx

    <data:DataGridTemplateColumn Header="Name">

                        <data:DataGridTemplateColumn.CellTemplate>

                            <DataTemplate>

                                <StackPanel Orientation="Horizontal">

                                    <TextBlock Padding="5,0,5,0"

                                        Text="{Binding FirstName}"/>

                                    <TextBlock Text="{Binding LastName}"/>

                                </StackPanel>

                            </DataTemplate>

                        </data:DataGridTemplateColumn.CellTemplate

                    </data:DataGridTemplateColumn>         

     

    Jim (http://jimmangaly.blogspot.com/)

    http://www.identitymine.com/
  • codebased

    codebased

    Participant

    826 Points

    386 Posts

    Re: Formating Data grid date column

    Apr 24, 2008 12:55 AM | LINK

    I've created a class called Converter1 and i am using the way it has been suggested. However, it is not working at all.

    public class Converter1 : IValueConverter

    {

    #region IValueConverter Members

    //Define the Convert method to change DateTime object to string

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

    {

    //value is the data from the source object

    DateTime thisdate = (DateTime)value;

    return thisdate.ToString("d");

    }

    //ConvertBack not implemented for OneWay binding

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

    {

    throw new NotImplementedException();

    }

    #endregion

    }

     

     

    <TextBlock Padding="5,0,5,0" Text="{Binding DateOfBirth, Converter={StaticResource Converter1}}" />

    "If I've answered your query then please mark it as "Answered".
  • Jim Mangaly

    Jim Mangaly

    Contributor

    2646 Points

    383 Posts

    Re: Formating Data grid date column

    Apr 24, 2008 05:44 AM | LINK

    The converter attribute in the binding syntax should point to an instance of the converter. Have you instantiated the converter in your XAML.

    This is how you do it:

    1) Add a namespace reference to the namespace in which  the converter class resides:

    <UserControl x:Class="BindingConverter.Page"

                      xmlns="http://schemas.microsoft.com/client/2007"                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

                      xmlns:local="clr-namespace:BindingConverter"

                        Width="400" Height="300">

    2) Next instantiate the converter in XAML:

    <UserControl.Resources>

           <local:Converter1 x:Key="key1"/>

    </UserControl.Resources>

    3) Now reference that instance of the converter in the binding syntax:

       Text="{Binding DateOfBirth, Converter={StaticResource key1}}" />

    Hope this helps.

    Jim (http://jimmangaly.blogspot.com/)

    http://www.identitymine.com/
  • codebased

    codebased

    Participant

    826 Points

    386 Posts

    Re: Formating Data grid date column

    Apr 24, 2008 06:33 AM | LINK

    Thank you Jim. It is working!!

     

    [B]cheers,

    Ricky

    "If I've answered your query then please mark it as "Answered".