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

GridViewMaskBox for time showing date too

2 Answers 71 Views
This is a migrated thread and some comments may be shown as answers.
Brandon asked on 26 Dec 2014, 02:06 PM
I'm attempting to setup a gridview in which one of the columns is a date-only field.  In order to achieve this, I'm using a gridViewMaskBox.  The below code shows a correct 00:00 AM on edit, but after the cell is no longer being edited, the field shows date and time.  Setting a FormatString for the column does not change this behavior.  Any help or suggestions?

            gridViewMaskBoxColumn1.EnableExpressionEditor = false;
            gridViewMaskBoxColumn1.FieldName = "From";
            //gridViewMaskBoxColumn1.FormatString = "{0:hh:mm tt}";
            gridViewMaskBoxColumn1.HeaderText = "From";
            gridViewMaskBoxColumn1.Mask = "t";
            gridViewMaskBoxColumn1.MaskType = Telerik.WinControls.UI.MaskType.DateTime;
            gridViewMaskBoxColumn1.Name = "From";
            gridViewMaskBoxColumn1.Width = 94;

2 Answers, 1 is accepted

Sort by
answered on 26 Dec 2014, 03:20 PM
The original post stated that I'm attempting to create a date-only field.  That is incorrect, I'm attempting to create a time-only field.  Sorry for the confusion!
Dess | Tech Support Engineer, Principal
Telerik team
answered on 30 Dec 2014, 04:30 PM
Hello Brandon,

Thank you for writing.

In order to achieve your goal, it is suitable to use a GridViewDateTimeColumn. Thus, you can specify the format of the text part by setting the FormatString property. Additionally, you can use the Format property to specify the desired format for the editor. Note that you can hide the arrow button appearing for the date time picker by using the CellEditorInitialized event. Here is a sample code snippet:
GridViewDateTimeColumn dateTimeColumn = new GridViewDateTimeColumn("DateTimeColumn");
dateTimeColumn.FormatString = "{0:hh:mm tt}";
dateTimeColumn.Format = DateTimePickerFormat.Custom;
dateTimeColumn.CustomFormat = "t";
dateTimeColumn.Width = 100;
private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
    RadDateTimeEditor timeEditor = e.ActiveEditor as RadDateTimeEditor;
    if (timeEditor!=null)
        RadDateTimeEditorElement el = timeEditor.EditorElement as RadDateTimeEditorElement;
        el.ArrowButton.Visibility = Telerik.WinControls.ElementVisibility.Collapsed;

I hope this information helps. Should you have further questions, I would be glad to help.

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

Asked by
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question