(RadGridView)(EditorRequired)(RadDateTimeEditor) How to define the Date format?

2 Answers 130 Views
GridView
WEI TZE
Top achievements
Rank 2
Iron
Iron
WEI TZE asked on 19 May 2023, 09:35 AM

Background:

  • I have a gridview column subscribing to EditorRequired event.
  • In the EditorRequired function, it will either prompt DropDownList or RadDateTimeEditor.

(Please refer attached for sample project)


 private void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
        {
            if (this.radGridView1.CurrentColumn.Name == "Value" && this.radGridView1.CurrentRow.Cells[2].Value.ToString() == "DropDown")
            {
                RadDropDownListEditor editor = new RadDropDownListEditor();
                RadDropDownListEditorElement element = editor.EditorElement as RadDropDownListEditorElement;
                element.DropDownStyle = RadDropDownStyle.DropDownList;
                element.AutoSizeItems = true;
                var selection = this.radGridView1.CurrentRow.Cells[3].Value.ToString().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
                element.DataSource = selection;

                e.Editor = editor;
            }
            else if (this.radGridView1.CurrentColumn.Name == "Value" && this.radGridView1.CurrentRow.Cells[2].Value.ToString() == "Date")
            {
                RadDateTimeEditor editor = new RadDateTimeEditor();
                editor.CustomFormat = "dd-MMM-yyyy";
                e.Editor = editor;
            }
        }

 

Question:

  • How to define the Date format?

I have tried to set the CustomFormat but the format remains as shown in the screenshot above.


 RadDateTimeEditor editor = new RadDateTimeEditor();
                editor.CustomFormat = "dd-MMM-yyyy";
                e.Editor = editor;

2 Answers, 1 is accepted

Sort by
1
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 22 May 2023, 08:59 AM

Hi, WEI TZE,

Thank you for the provided sample project. Indeed, the EditorRequired event is appropriate to specify what editor type to be used for the cell before entering edit mode. However, the appropriate place to specify the custom format is the CellEditorInitialized event:

        

public Form1() { InitializeComponent(); this.radGridView1.CellEditorInitialized += RadGridView1_CellEditorInitialized; }

private void RadGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e) { RadDateTimeEditor dateEditor = e.ActiveEditor as RadDateTimeEditor; if (dateEditor != null) { RadDateTimeEditorElement el = dateEditor.EditorElement as RadDateTimeEditorElement; el.Format = DateTimePickerFormat.Custom; el.CustomFormat = "dd-MMM-yyyy"; } }

The following help article is quite useful about understanding the editing lifecycle in RadGridView:

https://docs.telerik.com/devtools/winforms/controls/gridview/editors/editing-lifecycle 

Off topic, I have noticed that the serialized AutoScaleDimensions in the Designer.cs file indicates that the designer has been opened on higher than 100% DPI scaling which is not recommended:

 this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);

In order to produce as good layout as possible when running your application on monitors with different DPI scaling, it is important to have some thoughts in mind before you start designing the application: HDPI Tips and Tricks.

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

WEI TZE
Top achievements
Rank 2
Iron
Iron
commented on 22 May 2023, 09:26 AM | edited

Thanks Dess,

It works, but immediately after the cell end edit, the value changed to another format with Date and Time.

This is a general text column.

[Attached please find the sample project]

My current workaround is to add additional event handler at CellEndEdit - to change the Date format.

private void radGridView1_CellEndEdit(object sender, GridViewCellEventArgs e)
        {
            GridViewDataColumn column = e.Column as GridViewDataColumn;
            if (e.Row is GridViewDataRowInfo && column != null && column.Name == "Value")
            {
                if (e.Row.Cells["Type"].Value.ToString() == "Date")
                {
                    string s = e.Value.ToString();
                    var date = DateTime.ParseExact(s, "dd/M/yyyy hh:mm:ss tt",
                                                       CultureInfo.InvariantCulture);
                    e.Row.Cells["Value"].Value = date.ToString("dd-MMM-yyyy", CultureInfo.InvariantCulture);
                }
            }
        }

1
Dess | Tech Support Engineer, Principal
Telerik team
answered on 23 May 2023, 11:54 AM

Hello, WEI TZE,

Since the stored value within the cell is DateType inside a GridViewTextBoxColumn, the ActiveEditor.Value property will be returned when committing to the cell:

In type specific columns like GridViewDateTimeColumn, it is possible to specify the format for the column. However, the project scenario is very specific and different value types are being managed within the same column, it is necessary to apply the desired formatted value once the editor commits the value.

The demonstrated code in the CellEndEdit event is the appropriate way to apply the desired format for the DateTime values. Feel free to use it to achieve the desired behavior.

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
GridView
Asked by
WEI TZE
Top achievements
Rank 2
Iron
Iron
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or