Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. To format date in datagrid column in silverlight

To format date in datagrid column in silverlight

Scheduled Pinned Locked Moved WPF
helptutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    priyagee
    wrote on last edited by
    #1

    In my datagrid the date is displayed with time , how to format it and display in dd/MM/yyyy format . Help me please

    J 1 Reply Last reply
    0
    • P priyagee

      In my datagrid the date is displayed with time , how to format it and display in dd/MM/yyyy format . Help me please

      J Offline
      J Offline
      johnsontroye
      wrote on last edited by
      #2

      If you are binding to collection of custom objects such as Person and that class has a property such as BirthDate you could do the following: First, create DateTimeConverter class as such: Imports System.Windows.Data Public Class DateTimeConverter Implements IValueConverter Public Function Convert(ByVal value As Object, _ ByVal targetType As System.Type, _ ByVal parameter As Object, _ ByVal culture As System.Globalization.CultureInfo) As Object _ Implements System.Windows.Data.IValueConverter.Convert Dim DateValue As DateTime = value Return DateValue.ToShortDateString() End Function Public Function ConvertBack(ByVal value As Object, _ ByVal targetType As System.Type, _ ByVal parameter As Object, _ ByVal culture As System.Globalization.CultureInfo) As Object _ Implements System.Windows.Data.IValueConverter.ConvertBack Dim StrValue As String = value.ToString() Dim ResultDateTime As DateTime If DateTime.TryParse(StrValue, ResultDateTime) Then Return ResultDateTime End If Return value End Function End Class Next, add xmlns to to your projects namespace so you can get reference to your new class such as: xmlns:local="clr-namespace:Silverlight_DataGrid" Next, depending on what type of control you are using to bind to, add reference to your converter for date column. In this case, I have single column in grid, which is bound to DataGridTextColumn. The property in my class i'm binding to is BirthDate. <data:DataGrid x:Name="myDataGrid" Grid.Row="1" AlternatingRowBackground="AliceBlue" Background="WhiteSmoke" BorderBrush="DarkBlue" BorderThickness="2" CanUserResizeColumns="True" CanUserSortColumns="True" Opacity="8" AutoGenerateColumns="False"> <data:DataGrid.Columns> <data:DataGridTextColumn Binding="{Binding BirthDate, Converter={StaticResource DateConverter}}" /> </data:DataGrid.Columns> </data:DataGrid>

      Troy Johnson www.anothercodesite.com/blog

      P 1 Reply Last reply
      0
      • J johnsontroye

        If you are binding to collection of custom objects such as Person and that class has a property such as BirthDate you could do the following: First, create DateTimeConverter class as such: Imports System.Windows.Data Public Class DateTimeConverter Implements IValueConverter Public Function Convert(ByVal value As Object, _ ByVal targetType As System.Type, _ ByVal parameter As Object, _ ByVal culture As System.Globalization.CultureInfo) As Object _ Implements System.Windows.Data.IValueConverter.Convert Dim DateValue As DateTime = value Return DateValue.ToShortDateString() End Function Public Function ConvertBack(ByVal value As Object, _ ByVal targetType As System.Type, _ ByVal parameter As Object, _ ByVal culture As System.Globalization.CultureInfo) As Object _ Implements System.Windows.Data.IValueConverter.ConvertBack Dim StrValue As String = value.ToString() Dim ResultDateTime As DateTime If DateTime.TryParse(StrValue, ResultDateTime) Then Return ResultDateTime End If Return value End Function End Class Next, add xmlns to to your projects namespace so you can get reference to your new class such as: xmlns:local="clr-namespace:Silverlight_DataGrid" Next, depending on what type of control you are using to bind to, add reference to your converter for date column. In this case, I have single column in grid, which is bound to DataGridTextColumn. The property in my class i'm binding to is BirthDate. <data:DataGrid x:Name="myDataGrid" Grid.Row="1" AlternatingRowBackground="AliceBlue" Background="WhiteSmoke" BorderBrush="DarkBlue" BorderThickness="2" CanUserResizeColumns="True" CanUserSortColumns="True" Opacity="8" AutoGenerateColumns="False"> <data:DataGrid.Columns> <data:DataGridTextColumn Binding="{Binding BirthDate, Converter={StaticResource DateConverter}}" /> </data:DataGrid.Columns> </data:DataGrid>

        Troy Johnson www.anothercodesite.com/blog

        P Offline
        P Offline
        priyagee
        wrote on last edited by
        #3

        Thank You very much for ur reply and it works fine . Thank u again

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups