locked
Change Formate of date in datagrid RRS feed

  • Question

  • Hello Guys,

    I have been using a datagrid....

    I have data for date column in datagrid like this:-

    21.5.2012

    9.8.2012

    12.3.2012

    I just want to modify date formate for only first entry like "21.May.2012"

    Can anybody help me on this????

    Its urgent....Frown

    Wednesday, July 4, 2012 5:51 AM

Answers

  • Can you give me an example that how can I use IValueConvertor for change only first row item,

    try this sample, it might help you.

    <Converter:DateTimeConveter x:Key="DateTimeConveter"/>
      </navigation:Page.Resources>
    <sdk:DataGridTextColumn x:Name="clientDateStarted" Binding="{Binding Path=DateTime
     Converter={StaticResource DateTimeConveter}, Mode=TwoWay}"
     Header="Date Started" Width="SizeToHeader"/>
     
     
     public class DateTimeConveter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
    			if(parameter != null)
    			{
    				var myclass = parameter as Myclass; // your data class name
    				//Check the parameter.
    				if (myclass.Id.Equals(1))
    				{                
    					return value.ToString("dd.MMM.yyyy");
    				}
    				else
    				{
    					return null;
    				}
    			}
    			return null;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }



    Thursday, July 5, 2012 2:11 AM

All replies

  • I just want to modify date formate for only first entry like "21.May.2012"

    You can do it in many ways.

    1. while inserting data in itemsource for the datagrid, you can make date formatting, if the item is first, as given below.

    var str = DateTime.Now.ToString("dd.MMM.yyyy");

    2. you can make use of IValueConverter, pass some unique value as a parameter to converter to identify as first row item,  inlcude the above  logic in converter.

    let me know if you want anything else.

    Wednesday, July 4, 2012 12:16 PM
  • Hello Syed,

    Thanks for reply....

    Its true that I can use IValueConvertor but,

    Can you give me an example that how can I use IValueConvertor for change only first row item,

    cause I have been using a convertor but it applying to all items in to grid and my requirement is to change the formate of only one particular item you can say first item.

    Thursday, July 5, 2012 12:01 AM
  • You might have entered the binding of the date with the datagrid columns for eg:

      <navigation:Page.Resources>
    
    <Converter:DateTimeConveter x:Key="DateTimeConveter"/>
      </navigation:Page.Resources>
    
    
    
    <sdk:DataGridTextColumn x:Name="clientDateStarted" Binding="{Binding Path=DateTime
     Converter={StaticResource DateTimeConveter}, Mode=TwoWay}"
     Header="Date Started" Width="SizeToHeader"/>
    

    and converter 
    public class DateTimeConveter: IValueConverter
        {
     
            #region IValueConverter Members
     
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
     string format = "dd.MMM.yyyy";
           
    string date = value.ToString(format, DateTimeFormatInfo.InvariantInfo);
           
    return date;
             } 
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
               
            }
     
    }


    Thursday, July 5, 2012 1:22 AM
  • Can you give me an example that how can I use IValueConvertor for change only first row item,

    try this sample, it might help you.

    <Converter:DateTimeConveter x:Key="DateTimeConveter"/>
      </navigation:Page.Resources>
    <sdk:DataGridTextColumn x:Name="clientDateStarted" Binding="{Binding Path=DateTime
     Converter={StaticResource DateTimeConveter}, Mode=TwoWay}"
     Header="Date Started" Width="SizeToHeader"/>
     
     
     public class DateTimeConveter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
    			if(parameter != null)
    			{
    				var myclass = parameter as Myclass; // your data class name
    				//Check the parameter.
    				if (myclass.Id.Equals(1))
    				{                
    					return value.ToString("dd.MMM.yyyy");
    				}
    				else
    				{
    					return null;
    				}
    			}
    			return null;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }



    Thursday, July 5, 2012 2:11 AM