Share via

How to show only date in DateTime column?

Takeshi 41 Reputation points
Sep 9, 2022, 12:06 PM

Hi all,
how to show only Date (without Time) in DateTime column, in DataGrid?
I have created this:

Class (.cs)

class Car
{
public DateTime year { get; set;}
}

DataGrid (.xaml)

<DataGrid Name="dtgCar">
<DataGrid.Columns>
<DataGrid.TextColumn Header="Year" Binding="{Binding Path=year}" />
...

and ... (.cs)

Car c = new Car();
c.year = 1987;
dtgCar.Items.Add(c);

there, in datagrid column it shows me all date+time ... how to show only date?
thanks

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,843 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,337 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. satya karki 991 Reputation points MVP
    Sep 22, 2022, 7:24 AM

    Hi @Takeshi ,
    You can try StringFormat such as below.

    <Grid>  
                 <DataGrid x:Name="dtgCar" ItemsSource="{Binding Cars}" AutoGenerateColumns="False">  
                     <DataGrid.Columns>  
                         <DataGridTextColumn Header="year" Binding="{Binding Path=Year, StringFormat={}{0:dd/MM/yyyy}}"/>  
                     </DataGrid.Columns>  
                 </DataGrid>  
             </Grid>  
    

    Note: To control formatting in DataGrid Auto-generated columns should be false.

    1 person found this answer helpful.
    0 comments No comments

  2. Karen Payne MVP 35,556 Reputation points
    Sep 9, 2022, 12:18 PM

    If using .NET Core 6 there is DateOnly.

    public class Car  
    {  
        public int Id { get; set; }  
        public DateOnly Manufactured { get; set; }  
    }  
    

    Create an instance Car car = new () { Manufactured = new DateOnly(2022, 9, 1) };

    Note you can not change just Year aspect of a DateOnly, instead car.Manufactured = new DateOnly(2023, car.Manufactured.Month, car.Manufactured.Day);

    If not using .NET Core 6 than format the value in the grid in your binding use StringFormat=d


  3. Hui Liu-MSFT 48,641 Reputation points Microsoft External Staff
    Sep 12, 2022, 3:07 AM

    Xaml:

    <Grid>  
            <DataGrid x:Name="dtgCar" ItemsSource="{Binding Cars}" AutoGenerateColumns="False">  
                <DataGrid.Columns>  
                    <DataGridTextColumn Header="year" Binding="{Binding Path=Year, StringFormat = {}{0:yyyy}}"/>  
                </DataGrid.Columns>  
            </DataGrid>  
        </Grid>  
    

    Codebehind:

     public partial class MainWindow : Window  
        {  
            public ObservableCollection<Car> Cars { get; set; }  
            public MainWindow()  
            {  
      
                InitializeComponent();  
               Cars = new ObservableCollection<Car>();  
            Cars.Add(new Car() { Year = new DateTime(1987, 8, 18) });  
                DataContext = this;  
            }  
        }  
        public class Car  
        {  
            public DateTime Year { get; set; }  
        }  
    

    The result:

    239868-image.png

    ----------------------------------------------------------------------------

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.