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

[Solved] Grid need to set custom dateformat

11 Answers 267 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Daryl Shenner
Top achievements
Rank 1
Daryl Shenner asked on 01 Mar 2012, 04:13 PM

FYI :

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy/MM/dd}")]
[DataType(DataType.Date)]
public DateTime? ReceivedDate { get; set; }

How to change DataFormatString  at run time (dynamically)  based on user input Format.




11 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 06 Mar 2012, 01:08 PM
Hello Daryl,

The Grid keeps the format in a column object for the particular property. You could change the format in there.  For example:
var grid =$("#Grid").data("tGrid");
column = grid.columnFromMember("ReceivedDate");
column.format = "{0: dd/MM/yyyy}";
However, the new format won't be applied until the Grid is refreshed

All the best,
Daniel
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now.
0
Daryl Shenner
Top achievements
Rank 1
answered on 24 May 2012, 11:04 AM
I will get date format from DB.
I want to set it to the calender control which is inside calender control.

Note:
      Am able to display the date format . but am unable to set in for the calender control while editing.
0
Licenses
Top achievements
Rank 1
answered on 24 May 2012, 11:15 AM
Try setting the date format in the Culture and UICulture objects and turn on Telerik Globalization in the ScriptRegistrar?

code snippet showing what I mean: (You can ignore the return type - CultureModel)

private CultureModel ConfigureCulture()
       {
           //TODO: Get culture info from currently logged-in user
           var culture = new CultureInfo("nl-BE");
 
           // Some cultures appear to be read-only, so we clone the culture just to be sure
           var myCulture = (CultureInfo)culture.Clone();
 
           var cultureSettings = new CultureModel()
           {
               DecimalSeperator = ",",
               ThousandsSeperator = ".",
               CurrencyCode = "EUR",
               DateTimeFormat = "dd/MM/yyyy hh:mm"
           };
 
           myCulture.NumberFormat.CurrencySymbol = cultureSettings.CurrencyCode;
           myCulture.NumberFormat.CurrencyDecimalSeparator = cultureSettings.DecimalSeperator;
           myCulture.NumberFormat.CurrencyGroupSeparator = cultureSettings.ThousandsSeperator;
           myCulture.NumberFormat.NumberDecimalSeparator = cultureSettings.DecimalSeperator;
           myCulture.NumberFormat.NumberGroupSeparator = cultureSettings.ThousandsSeperator;
           myCulture.NumberFormat.PercentDecimalSeparator = cultureSettings.DecimalSeperator;
           myCulture.NumberFormat.PercentGroupSeparator = cultureSettings.ThousandsSeperator;
           myCulture.NumberFormat.CurrencyPositivePattern = 3;
           myCulture.NumberFormat.CurrencyNegativePattern = 3;
 
 
           System.Threading.Thread.CurrentThread.CurrentCulture = myCulture;
           System.Threading.Thread.CurrentThread.CurrentUICulture = myCulture;
 
           return cultureSettings;
       }
0
Daryl Shenner
Top achievements
Rank 1
answered on 24 May 2012, 12:19 PM
Can you have sample projects.
0
Daryl Shenner
Top achievements
Rank 1
answered on 24 May 2012, 01:17 PM
can you have sample projects
0
Daryl Shenner
Top achievements
Rank 1
answered on 25 May 2012, 05:23 AM
any one can send sample project
0
Daryl Shenner
Top achievements
Rank 1
answered on 28 May 2012, 05:47 AM
any one look up this issue. 
0
Daniel
Telerik team
answered on 29 May 2012, 09:56 AM
Hello Daryl,

Enabling globalization and changing the date format should work. Also, if you are using the ModelBinder you should add a custom binder which takes the current culture into account when parsing dates in order to avoid a ModelState error when editing(the default one will use invariant culture). I attached a sample project which implements this scenario.

Regards,
Daniel
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now.
0
Daryl Shenner
Top achievements
Rank 1
answered on 15 Jun 2012, 01:11 PM
Hi,
I've tried your sample. It was working fine. But my problem was that I've the DateTime column as Nullable. Initially in your solution, in the constructor, a default value is been passed for the "Date" model. In my case, this "Date" model is nullable DateTime type. When I use dd/MM/yyyy as Date Format I get the below error "The value '28/06/2012' is not valid for Date.". 

Please have a look into the ("Error.jpg")attached screen shot. 

Also, the modified solution has been attached. Please provide your solution taking it.

Awaiting for your quick reply.


 
0
Daryl Shenner
Top achievements
Rank 1
answered on 18 Jun 2012, 05:17 AM
any body look at this issue.
0
Daniel
Telerik team
answered on 20 Jun 2012, 06:22 AM
Hi Daryl,

The problem occurs because the custom model binder is registered only for the DateTime type in the sample project. You should register it also for the DateTime? type in order to support dynamic date formats for Nullable dates e.g.

protected void Application_Start()
{
    ModelBinders.Binders.Add(typeof(DateTime), new DateTimeModelBinder());
    ModelBinders.Binders.Add(typeof(DateTime?), new DateTimeModelBinder());
I attached the modified project.

Regards,
Daniel
the Telerik team
Check out the successor of Telerik MVC Extensions - Kendo UI for ASP.NET MVC - and deem it for new ASP.NET MVC development.
Tags
Grid
Asked by
Daryl Shenner
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Daryl Shenner
Top achievements
Rank 1
Licenses
Top achievements
Rank 1
Share this question
or