[UWP] [EF] How to format DateTime column in UWP DataGrid

BitSmithy 1,951 Reputation points
2020-03-17T15:48:39.02+00:00

Hello,

I have DataGrid, and I use AutoGenerateColumns=true;
For getting data from SQLite database I use EntityFramework (EF)

How to format DataTime kolumn? I want CreateDate column to be formated as "YYYY/MM/DD"
Below I shows my DbContext, and class definition.

public class MyClassDbContext : DbContext 
{
    public string connectionString;

    public DbSet<MyClass > Passes { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite(connectionString);
    }
}


public class MyClass : INotifyPropertyChanged
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id{get; set;}

    public DateTime CreateDate{get; set;}

}

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,306 Reputation points
    2020-03-17T18:23:13.727+00:00

    Hi,
    you can use converter like in following demo.

    XAML:

    <Page
        x:Class="App1.Page01"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App01"
        xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
      <Page.DataContext>
        <local:ViewModel/>
      </Page.DataContext>
      <Page.Resources>
        <local:DateConverter x:Key="conv"/>
      </Page.Resources>
      <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <controls:DataGrid ItemsSource="{Binding View}" AutoGenerateColumns="False">
          <controls:DataGrid.Columns>
            <controls:DataGridTextColumn Binding="{Binding CreateDate, Converter={StaticResource conv}}"/>
          </controls:DataGrid.Columns>
        </controls:DataGrid>
      </Grid>
    </Page>
    

    And classes:

    using System;
    using System.Collections.Generic;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Data;
    
    namespace App01
    {
      public class ViewModel
      {
        public ViewModel() => LoadData();
    
        List<Data> col = new List<Data>();
        public List<Data> View { get => col; }
    
        private void LoadData()
        {
          for (int i = 1; i < 10; i++) col.Add(new Data() { Id = i, CreateDate = DateTime.Now.AddDays(i) });
        }
      }
      public class Data
      {
        public int Id { get; set; }
        public DateTime CreateDate { get; set; }
      }
    
      public class DateConverter : IValueConverter
      {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
          var d = value as DateTime?;
          if (!d.HasValue) return value;
          return d.Value.ToString("yyyy/MM/dd");
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
          throw new NotImplementedException();
        }
      }
    }
    

0 additional answers

Sort by: Most helpful