How to show only date in DateTime column?

Takeshi 41 Reputation points
2022-09-09T12:06:04.03+00:00

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

Developer technologies Windows Presentation Foundation
Developer technologies C#
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. satya karki 996 Reputation points MVP
    2022-09-22T07:24:43.44+00:00

    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,586 Reputation points Volunteer Moderator
    2022-09-09T12:18:16.873+00:00

    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,676 Reputation points Microsoft External Staff
    2022-09-12T03:07:00.69+00:00

    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.