0

I have a DataTable that is displayed in a WPF grid view. All the columns are typed. There is a column of type DateTime that when I display the table in the GUI shows the dates in MM/dd/yyy format, I need to change the format since for my users that is very much prone to error. The desired format is dd/MM/yyyy

Example code:

DataTable dt = new DataTable();
dt.Columns.Add("date", typeof(DateTime));
dt.Columns.Add("val", typeof(double));

GridView gv = new GridView();
gv.ItemsSource = dt.DefaultView;

There are quite some questions relate to this one, but none actually solves the problem, just convert the content to string. (ie here) Instead of modifying the DataTable to the format expected (or the GridView)

enter image description here The users are supposed to enter the dates in the GridView.

Community
  • 1
  • 1
Santi Peñate-Vera
  • 1,053
  • 4
  • 33
  • 68
  • `String.Format("Date: {0:dd-MM-yyyy} Time: {0:HH:mm:ss:fffff} ",DateTime.Now);` Would return e.g. "Date: 21-03-2016 Time 15:23:33" – Nitro.de Mar 21 '16 at 14:22
  • Where would you use that code?, remember that I'm not printing in console or whatever, I need the DataTable content to be displayed with the format I specify. – Santi Peñate-Vera Mar 21 '16 at 14:26
  • I can't tell you because there is too less code here. I just see some controls and 2 colums. Is the whole DataGrid TwoWay? Are you binding anything in XAML or is all code behind? – Nitro.de Mar 21 '16 at 14:29
  • I use no especial XAML at all, and the code snippet is fully representative of the actual code. Posting the complete code would just confuse the people. What you see in the picture is what the user sees. – Santi Peñate-Vera Mar 21 '16 at 14:30
  • You should Use MVVM and do something simple like `Binding="{Binding Date, StringFormat={}\{0:dd/MM/yyyy hh:mm\}}`. Now if you use code behind you've to catch the users date input and use `DateTime.TryParse` and format their input in what you need. – Nitro.de Mar 21 '16 at 14:33
  • To use XAML implies that I bind a table before execution and that is not possible in my case, otherwise I need a fully detail explanation of how to bind the table in runtime while keeping the XAML function (which I just tried and fails to compile) It tells me that the Columns do not exist... – Santi Peñate-Vera Mar 21 '16 at 14:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/106921/discussion-between-nitro-de-and-santi-penate-vera). – Nitro.de Mar 21 '16 at 14:43

2 Answers2

2

In your xaml code, change the GridView not to auto generate the columns. Then you add the columns in the xaml code. When specifieng the binding, you can setup a stringformat. Here is an example how I did this:

 <DataGrid Name="CompetitionList" Grid.Column="0" Grid.Row="1" ItemsSource="{Binding CompetitionList}"
              SelectedItem="{Binding SelectedCompetition}" AutoGenerateColumns="False" CanUserAddRows="False"
              CanUserDeleteRows="False" BeginningEdit="CompetitionList_OnBeginningEdit">

        <DataGrid.Columns>
            <DataGridTextColumn Width="3*" Header="{lex:Loc Scrutinus:Text:Title}" Binding="{Binding Title}" />


            <DataGridTextColumn Width="1*" Header="{lex:Loc Scrutinus:Text:Starttime}"
                                Binding="{Binding StartTime, StringFormat={}{0:dd.MM.yyyy HH:mm}}" />

        </DataGrid.Columns>

    </DataGrid>
unkreativ
  • 482
  • 2
  • 8
  • Thats exactly why I asked OP if he's using xaml what he's not else great answer. – Nitro.de Mar 21 '16 at 14:31
  • The problem with XAML is that my table is generated by an instance of a class, therefore the table is not in memory there always. – Santi Peñate-Vera Mar 21 '16 at 14:33
  • @SantiPeñate-Vera You shouldn't worry about that. Just bind the ItemsSource of the Datagrid as soon as you've created what you need. Once set as ItemSource the object you bound wont get destroyed – Nitro.de Mar 21 '16 at 14:35
0

After hours of search, behold:

CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("es-ES");
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("es-ES");
// Put the following code before InitializeComponent()
Thread.CurrentThread.CurrentUICulture = new CultureInfo("es-ES");
Thread.CurrentThread.CurrentCulture = new CultureInfo("es-ES");
FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(System.Windows.Markup.XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));

This amazing piece of code must be in you main application form, and it'll change the culture of all the UI elements to the one you set. This will work for .net 4.5 and above.

See here for more info.

Community
  • 1
  • 1
Santi Peñate-Vera
  • 1,053
  • 4
  • 33
  • 68