DateTime Format when using SqldataReader

3 posts, 1 answers
  1. Rob
    Rob avatar
    59 posts
    Member since:
    Jan 2012

    Posted 21 Nov 2012 Link to this post

    I really am getting my support monies worth. sorry guys , I am trying to resolve before posting!!

    I have spent the morning reading the existing forum posts on this subject and the documentation, but nothing has helped so far.

    I have an application which is required to be in different languages ( select-able by the user) however they want the date time columns to always be in the same format (Swedish Culture)
     
    I am using  "Thread.CurrentThread.CurrentUICulture.Name"  for setting and checking which language I should be providing, however I want the date time columns in the grid to ignore this and always be same format. 

    I think because I am using unbound Gridview , the setting of the columns formatting does not work?  (FormatString , FormatInfo etc)   I am creating the grid by using  "RadGridView1.MasterTemplate.LoadFrom(Callsreader)" and loading an SQLDataReader

    The closest I found was this post...which shows having to change the culture in the datatable before setting to the Gridview.  I cant do this with an SQLDatareader  ( that I have found!) and I tried converting to using Datatables , however this started to cause numerous issues even without adding the convert Datatable part! 

    What is the way that should work?  Is there a way to set the  GridView UICulture to always be Swedish ? That would be an easy way!

    Many thanks

    Rob

  2. Answer
    Svett
    Admin
    Svett avatar
    728 posts

    Posted 23 Nov 2012 Link to this post

    Hi Rob,

    You can retain the culture for the GridViewDateTimeColumn by creating a custom cell in the following manner:

    public class MyGridDateTimeCellElement : GridDateTimeCellElement
            {
                public MyGridDateTimeCellElement(GridViewColumn col, GridRowElement row)
                    : base(col, row)
                {
     
                }
     
                protected override Type ThemeEffectiveType
                {
                    get { return typeof(GridDateTimeCellElement); }
                }
     
                protected override string ApplyFormatString(object value)
                {
                    CultureInfo uiCultureInfo = Thread.CurrentThread.CurrentUICulture;
                    CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
     
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo("se-SE");
                    Thread.CurrentThread.CurrentCulture = new CultureInfo("se-SE");
                    string result = base.ApplyFormatString(value);
                    Thread.CurrentThread.CurrentCulture = cultureInfo;
                    Thread.CurrentThread.CurrentUICulture = uiCultureInfo;
                    return result;
                }
            }

    Then you should use the CreateCell event to replace the default one:

    void radGridView1_CreateCell(object sender, GridViewCreateCellEventArgs e)
    {
        if (e.CellType == typeof(GridDateTimeCellElement))
        {
            e.CellType = typeof(MyGridDateTimeCellElement);
        }
    }

    In addition, you can change the culture of the date time editor by using the CellEditorInitialized event in the following manner:

    private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
    {
        RadDateTimeEditor editor = e.ActiveEditor as RadDateTimeEditor;
        if (editor != null)
        {
            RadDateTimeEditorElement element = editor.EditorElement as RadDateTimeEditorElement;
            element.Culture = new CultureInfo("se-SE");
        }
    }

    I hope this helps.

    Kind regards,
    Svett
    the Telerik team
    Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Rob
    Rob avatar
    59 posts
    Member since:
    Jan 2012

    Posted 25 Nov 2012 Link to this post

    Thanks Svett

    Yes this did the trick perfectly. ( I changed  "se-SE" to "sv-SE" )

    Many thanks to you and your team 

    Rob
Back to Top