1

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.

7
  • 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" Commented Mar 21, 2016 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. Commented Mar 21, 2016 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? Commented Mar 21, 2016 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. Commented Mar 21, 2016 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. Commented Mar 21, 2016 at 14:33

2 Answers 2

3

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>
Sign up to request clarification or add additional context in comments.

3 Comments

Thats exactly why I asked OP if he's using xaml what he's not else great answer.
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.
@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
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.