Date formatting in WPat datagrid

I was already looking for stackoverflow for a solution and found this:

Need to format dates in dynamically generated WPF DataGrid

My problem is that I am loading some data from my SQL-Server database and want to show it in my WPF application in DataGrid. It works very well. The only thing I want to change is that the date column is in the format "DD / MM / YYYY HH: MM: SS" and I want to have "DD.MM.YYYY". Ok, then I looked at the link below and tried this in my program:

<Grid Width="648" Height="263"> <Grid.ColumnDefinitions> <ColumnDefinition Width="172*" /> <ColumnDefinition Width="90*" /> <ColumnDefinition Width="386*" /> </Grid.ColumnDefinitions> <DataGrid Name="dgBuchung" Height="213" HorizontalAlignment="Left" Margin="30,16,0,0" VerticalAlignment="Top" Width="595" AutoGenerateColumns="True" ItemsSource="{Binding}" Grid.ColumnSpan="3" Foreground="Black" BorderBrush="#FF688CAF" Opacity="1" Background="White" BorderThickness="1" > <!-- <ab:DataGridTextColumn Header="Fecha Entrada" Width="110" Binding="{Binding date, StringFormat={}{0:dd/MM/yyyy}}" IsReadOnly="True" /> --> </DataGrid> </Grid> 

The commented part is my solution, but it throws an XMLParseException. First of all, is such a solution possible when using AutoGenerateColumns? If not, how else can I try to handle this? If so, what is the problem with the code above?

EDIT: My question is not resolved because I decided not to rebuild my application, is there no solution with AutoGenerateColumns = true?

+44
c # wpf datagrid date-formatting
Nov 02 2018-11-11T00:
source share
5 answers

Remember to use DataGrid.Columns, all columns must be inside this collection. In my project, I formatted the date in a slightly different way:

 <tk:DataGrid> <tk:DataGrid.Columns> <tk:DataGridTextColumn Binding="{Binding StartDate, StringFormat=\{0:dd.MM.yy HH:mm:ss\}}" /> </tk:DataGrid.Columns> </tk:DataGrid> 

With AutoGenerateColumns, you cannot format the contol, because DataGird will add its own columns.

+82
Nov 02 2018-11-11T00:
source share
— -

It's very late to the party here, but in case someone else stumbles on this page ...

You can do this by setting the AutoGeneratingColumn handler in XAML:

 <DataGrid AutoGeneratingColumn="OnAutoGeneratingColumn" ..etc.. /> 

And then, behind the code of the code, do something like this:

 private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) { if (e.PropertyType == typeof(System.DateTime)) (e.Column as DataGridTextColumn).Binding.StringFormat = "dd/MM/yyyy"; } 
+60
May 14 '13 at 23:20
source share

If your related property is DateTime, then you only need to

 Binding={Property, StringFormat=d} 
+12
Apr 03 '13 at 20:33
source share

I know the accepted answer is pretty old, but there is a way to control formatting using AutoGeneratColumns:

First, create a function that will run when the column is generated:

 <DataGrid x:Name="dataGrid" AutoGeneratedColumns="dataGrid_AutoGeneratedColumns" Margin="116,62,10,10"/> 

Then check if the type of the generated column is DateTime and just change its String format to "d" to remove the temporary part:

 private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) { if(YourColumn == typeof(DateTime)) { e.Column.ClipboardContentBinding.StringFormat = "d"; } } 
0
Jun 09 '17 at 14:21
source share

select datagrid first and then go to find Datagrid_AutoGeneratingColumn properties and double click And then use this code

  Datagrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) { if (e.PropertyName == "Your column name") (e.Column as DataGridTextColumn).Binding.StringFormat = "dd/MMMMMMMMM/yyyy"; if (e.PropertyName == "Your column name") (e.Column as DataGridTextColumn).Binding.StringFormat = "dd/MMMMMMMMM/yyyy"; } 

I try, it works on WPF

0
Aug 29 '17 at 10:54 on
source share



All Articles