This is a migrated thread and some comments may be shown as answers.

DateTime Format when using SqldataReader

2 Answers 195 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Rob
Top achievements
Rank 2
Iron
Veteran
Iron
Rob asked on 21 Nov 2012, 10:55 PM
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 Answers, 1 is accepted

Sort by
0
Accepted
Svett
Telerik team
answered on 23 Nov 2012, 02:05 PM
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.
0
Rob
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 26 Nov 2012, 01:33 AM
Thanks Svett

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

Many thanks to you and your team 

Rob
Tags
GridView
Asked by
Rob
Top achievements
Rank 2
Iron
Veteran
Iron
Answers by
Svett
Telerik team
Rob
Top achievements
Rank 2
Iron
Veteran
Iron
Share this question
or