Tom
Tom

Reputation: 1763

WPF GridView: Changing the DateTime formatting for bound DataTable

We're using a WPF GridView that is dynamically bound to a DataTable (which in turn is the direct result of a DB query). This basically works fine and displays whatever the results from the DB query are in a nice table.

Now the problem is that some of the results contain DateTime columns, and the date displayed is always in US format and not in the user specified date format.

Is there an easy way to set the datetime formatting for the GridView display?

We don't know beforehand which columns will be DateTime as there are a number of different tables that are displayed.

The way we fill the gridview is as follows:

        MyListView.DataContext = data;
        MyListView.SetBinding(ListView.ItemsSourceProperty, new Binding());
        MyGridView.Columns.Clear();

        foreach (DataColumn col in data.Columns)
        {
            GridViewColumn gvcol = new GridViewColumn();
            gvcol.Header = col.ColumnName;
            gvcol.DisplayMemberBinding = new Binding(col.ColumnName);
            MyGridView.Columns.Add(gvcol);
        }

Upvotes: 1

Views: 2432

Answers (1)

Jonke
Jonke

Reputation: 6533

It should be enough to put the current culture in the applucation startup

/// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public App()
        {
            FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));

        }
    }

Upvotes: 2

Related Questions