Phia-CM
Phia-CM

Reputation: 67

Formatting the data for a column in a datagrid

I have a data base that holds users and their birthdays. I store the birthday in my data base as a DateTime Object.

Here is my XAML code

<DataGrid Name="lookAtUsers" ItemsSource={"Binding MyUsers"}>
    <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
    <DataGridTextColumn Header="Birthday!" Binding="{Binding Birthday}"/>
</DataGrid>

When the DataGrid is displayed, the time is displayed with it.

There seems to be two possible solutions:

1.) Format is so that when I save this information I save a DateTime object that is formatted as MM/dd/yyyy

2.) be able to some how format the text in the DataGrid so that I can remove the time

I thought that option one would be the easiest to implement but so far I have not been able to find a solution online.

Option two is more likely a little more complicated and since I am a new C# programmer, I do not know much about DataGrids

Upvotes: 0

Views: 46

Answers (1)

Phia-CM
Phia-CM

Reputation: 67

Turns out you can format a string format of a column as the following:

Binding="{Binding Birthday, StringFormat=\{0:dd/MM/yyyy\}}"

I do not really understand why this works so if anyone wants to take the time to explain or explain option 2 that would be awesome

Upvotes: 1

Related Questions