locked
Datagrid Column Date Formatting Issue while AutoGenerateColumns=True RRS feed

  • Question

  • Hello there,

    I've got very strange issue in DateColumn while binding my datagrid.

    :: AutoGeneratColumns=True in my dataGrid.

    now, when i bind data to dataGrid, my system DateFormat is "dd-mm-yyyy hh:mm:ss" and when i see bound DataGrid

    it shows me in "MM/dd/yyyy hh:mm:ss t" Format. The same DataTable i Bind to the COmbobox then the ComboBox shows me the sytem Format. u see? in ComboBox, dateFormat comes perfect. 

    So why dataGrid behave such odd with Dates while AutoGenerateColumn is set to True

    (Note that i'm using my custom dataGrid here, but i didnt customized columns classes??)

    Any help will be appreciated, 

    Thanks. :)

    Monday, December 1, 2014 5:01 AM

Answers

  • The DataGrid uses the en-US date format by default, regardless of your system settings, but you could handle the AutoGeneratingColumn event for the DataGrid and set a string format for the binding:

        private void dataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) {
    
          if (e.PropertyName == "time") {
            DataGridTextColumn column = e.Column as DataGridTextColumn;
            Binding binding = column.Binding as Binding;
            binding.StringFormat = "dd-MM-yyyy hh:mm:ss";
          }
        }
    


    <DataGrid x:Name="dataGrid" AutoGeneratingColumn="dataGrid_AutoGeneratingColumn"/>

    In the above example, the column or property contanining the DateTime value is named "time" so you should change this to the actual name of your column or property.

    Please remember to mark helpful posts as answer and/or helpful.

    • Marked as answer by MayurPaghdal Tuesday, December 2, 2014 4:40 AM
    Monday, December 1, 2014 11:35 AM

All replies

  • Hi

    I can see both datagrid datetime column and combobox displaying same datetime format as my system format, i don't see why you have issue with datetime format.

    if possible share your code.


    Thanks & Regards
    Syed Amjad Sr. Silverlight/WPF Developer,
    yahoo : syedamjad6736@yahoo.com, skype : syedamjad.0786.
    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    Monday, December 1, 2014 9:10 AM
  • WPF controls don't all format to your language settings by default.

    You could try pasting this into your app.xaml.cs:

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
    
        // Set application startup culture based on config settings
        string culture = ConfigurationManager.AppSettings["Culture"];
        SetCulture(culture);
    
        Theme = ConfigurationManager.AppSettings["Theme"];
        SetTheme(Theme);
    
        FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement),
        new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));            
    }

    from

    http://weblog.west-wind.com/posts/2009/Jun/14/WPF-Bindings-and-CurrentCulture-Formatting


    Hope that helps
    Please don't forget to upvote posts which you like and mark those which answer your question.

    Monday, December 1, 2014 9:31 AM
  • The DataGrid uses the en-US date format by default, regardless of your system settings, but you could handle the AutoGeneratingColumn event for the DataGrid and set a string format for the binding:

        private void dataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) {
    
          if (e.PropertyName == "time") {
            DataGridTextColumn column = e.Column as DataGridTextColumn;
            Binding binding = column.Binding as Binding;
            binding.StringFormat = "dd-MM-yyyy hh:mm:ss";
          }
        }
    


    <DataGrid x:Name="dataGrid" AutoGeneratingColumn="dataGrid_AutoGeneratingColumn"/>

    In the above example, the column or property contanining the DateTime value is named "time" so you should change this to the actual name of your column or property.

    Please remember to mark helpful posts as answer and/or helpful.

    • Marked as answer by MayurPaghdal Tuesday, December 2, 2014 4:40 AM
    Monday, December 1, 2014 11:35 AM