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

Format Date Column in edit Mode

8 Answers 334 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Doug Lott
Top achievements
Rank 1
Doug Lott asked on 09 Sep 2008, 09:36 PM
By default, the grid uses a RadDateTimePicker (I presume) when a column has a system type of DateTime.  Using the FormatString property we are able to format the date the way we wish while the grid is in view mode; but when a row is put into edit mode, the RadDateTimePicker shows the default Long date.  How can this be changed to show a Short date?

8 Answers, 1 is accepted

Sort by
0
Nikolay
Telerik team
answered on 12 Sep 2008, 04:06 PM
Hi Doug Lott,

I answered your support ticket on the same topic. I am pasting the answer here as well:

In order to change the date-time format in the RadDateTimeEditor itself, you should subscribe to the EditorRequired event. This event is fired whenever an editor is created and displayed. Then, in the event handler you can customize the date-time format by setting either the Format or the CustomFormat properties:
 
private void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)     
{     
    RadDateTimeEditor dateTimeEditor = e.Editor as RadDateTimeEditor;     
    if (dateTimeEditor != null)     
    {     
        ((RadDateTimePickerElement)dateTimeEditor).Format = DateTimePickerFormat.Short;     
        //or     
        //dateTimeEditor.CustomFormat = "dd/mm/yyyy";     
    }     
}    
 

If you have additional questions, feel free to contact me.

Greetings,
Nikolay
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Bill
Top achievements
Rank 1
answered on 17 Feb 2009, 08:50 PM
When I try to format my control for Time entry only, I get errors trying to get rid of the dropdown...
void grd_EditorRequired(object sender, EditorRequiredEventArgs e)  
{  
    if (e.EditorType == typeof(RadDateTimeEditor))  
    {  
         if (grd.CurrentCell.FormatString == "{0:HH:mm}")  
         {  
              ((RadDateTimePickerElement)e.Editor).Format = DateTimePickerFormat.Custom;  
              ((RadDateTimePickerElement)e.Editor).CustomFormat = "HH:mm";  
              ((RadDateTimePickerElement)e.Editor).ShowUpDown = true; //<--ERROR  
         }  
         else  
              ((RadDateTimePickerElement)e.Editor).Format = DateTimePickerFormat.Short;  
     }  
     if (e.EditorType == typeof(RadComboBoxEditor))  
     {  
         ((RadComboBoxEditor)e.Editor).DropDownWidth = 180;  
     }  

 

If I comment out the line marked "ERROR" above, it works, however the calendar drops down when the user wants to enter the time.

 

The exact error, which happens when the user trys to edit the cell, looks like this:

{"Object reference not set to an instance of an object."}

at Telerik.WinControls.UI.RadDateTimeEditor.EndInit()  
at Telerik.WinControls.UI.BaseGridBehavior.InitializeEditor(GridViewColumn column)  
at Telerik.WinControls.UI.BaseGridBehavior.OnMouseDownLeft(MouseEventArgs e)  
at Telerik.WinControls.UI.BaseGridBehavior.OnMouseDown(MouseEventArgs e)  
at Telerik.WinControls.UI.RadGridView.OnMouseDown(MouseEventArgs e)  
at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)  
    

Thanks for any assistance you can provide.
0
Martin Vasilev
Telerik team
answered on 19 Feb 2009, 03:15 PM
Hello Bill,

Thank you for writing.

I confirm that there is an issue if you try to change the default drop down button to a spin button. We will address it in some of the next releases. Currently, you can just remove the drop down button and in this way prevent showing the calendar when you want to edit only times. Please, review the code-block below:

        private void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)  
        {  
            if (e.EditorType == typeof(RadDateTimeEditor))  
            {  
                ((RadDateTimeEditor)e.Editor).CustomFormat = "HH:mm";  
                ((RadArrowButtonElement)((RadDateTimeEditor)e.Editor).Children[1].Children[1]).Visibility =  
                    Telerik.WinControls.ElementVisibility.Hidden;  
            }  
        } 

I have updated your Telerik points for bringing our attention to this matter.

Please do not hesitate to contact me again if you have other questions.

All the best,
Martin Vasilev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Bill
Top achievements
Rank 1
answered on 19 Feb 2009, 03:51 PM
We succedded in making the dropdown go away. Thx...

However the issue with the custom 24Hr format remains.  I have StartDate, StartTime, EndDate, and EndTime columns - all RadDateTimePickers in my grid.  You can see in the code I've provided that I'm checking the format string to see if it should be formatted as a time or date.

The problem I'm having is, once the user edits a time field in the grid, all the DateTime cells in the grid get formatted as time from then on. Should I not be using the current cell's format string? ... if (grd.CurrentCell.FormatString == "{0:HH:mm}")  
0
Bill
Top achievements
Rank 1
answered on 19 Feb 2009, 04:04 PM
The grid's CurrentCell property is not the problem apparently. The EditorRequired event steps through and formats the date as data but on the screen the date column is showing as a time.
0
Bill
Top achievements
Rank 1
answered on 19 Feb 2009, 06:30 PM
...for all to see, Telerik has provided this solution (via ticket) which is working:

if (grd.CurrentCell.FormatString == "{0:HH:mm}")  
{  
   ((RadDateTimePickerElement)e.Editor).Format = DateTimePickerFormat.Custom;  
   ((RadDateTimePickerElement)e.Editor).CustomFormat = "HH:mm";  
   ((RadArrowButtonElement)((RadDateTimeEditor)e.Editor).Children[1].Children[1]).Visibility = ElementVisibility.Hidden;     
}  
else  
{  
   ((RadDateTimeEditor)e.Editor).CustomFormat = string.Empty;  
   ((RadDateTimePickerElement)e.Editor).Format = DateTimePickerFormat.Short;  
   ((RadArrowButtonElement)((RadDateTimeEditor)e.Editor).Children[1].Children[1]).Visibility = ElementVisibility.Visible;  

Thanks.
0
Rajaram
Top achievements
Rank 1
answered on 05 Jun 2018, 03:57 PM

How do Format Date Column in Edit Mode of the grid. I am using the WPF controls. I have to display the date values in 'dd/MM/yyyy' format in edit mode. I see there is no EditorRequiredEvent in case of WPF controls.So how do I change the format in these case?

My problem is when I click on the cell, the RadDateTimePicker considers the date as MM/dd/yyyy and it displays empty value. For example 13/05/2018 will show empty because 13 is not a valid month. So how to change this so that the it shows the required date format in Edit mode? Please help

 

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 06 Jun 2018, 10:48 AM
Hello, Rajaram,  

I would like to note that this forum is related to the Telerik UI for WinForms suite. However, it seems that your question is related to WPF. Feel free to post your technical questions in the relevant forum: https://www.telerik.com/forums

Thus, the WPF's community will gladly assist you.

I hope this information helps.Regards,
Dess
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
GridView
Asked by
Doug Lott
Top achievements
Rank 1
Answers by
Nikolay
Telerik team
Bill
Top achievements
Rank 1
Martin Vasilev
Telerik team
Rajaram
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or