New to Telerik UI for WinForms? Download free 30-day trial

GridViewDateTimeColumn

GridViewDateTimeColumn provides date entry and formatting for DateTime data types. You may enter the date and time directly into the editor or drop down the calendar to choose a date. The FormatString property sets the format of the date when the date is not currently being edited. The CustomFormat property is used to format the date once the user clicks on the cell to invoke the editor.

Figure 1: GridViewDateTimeColumn in Edit Mode

WinForms RadGridView GridViewDateTimeColumn in Edit Mode

Formatting the date.

GridViewDateTimeColumn dateTimeColumn = new GridViewDateTimeColumn();
dateTimeColumn.Name = "DateTimeColumn";
dateTimeColumn.HeaderText = "Order date";
dateTimeColumn.FieldName = "OrderDate";
dateTimeColumn.FormatString = "{0:D}";

Dim dateTimeColumn As New GridViewDateTimeColumn()
dateTimeColumn.Name = "DateTimeColumn"
dateTimeColumn.HeaderText = "Order date"
dateTimeColumn.FieldName = "OrderDate"
dateTimeColumn.FormatString = "{0:D}"

The formatting for date and time values also responds to globalization settings.

System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fr-BE");
GridViewDateTimeColumn dateTimeColumn = new GridViewDateTimeColumn();
dateTimeColumn.Name = "DateTimeColumn";
dateTimeColumn.HeaderText = "Order date";
dateTimeColumn.FieldName = "OrderDate";
dateTimeColumn.FormatString = "{0:D}";
radGridView1.MasterTemplate.Columns.Add(dateTimeColumn);

System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("fr-BE")
        Dim dateTimeColumn As New GridViewDateTimeColumn()
        dateTimeColumn.Name = "DateTimeColumn"
        dateTimeColumn.HeaderText = "Order date"
        dateTimeColumn.FieldName = "OrderDate"
        dateTimeColumn.FormatString = "{0:D}"
        RadGridView1.MasterTemplate.Columns.Add(dateTimeColumn)

The code below demonstrates how to change date formatting in edit mode (while the cell is being edited). There are two ways to achieve that - by setting the Format property of the column to Custom and the CustomFormat property of the column to a desired format, or by setting the same properties of the editor itself. Please note that we are using the CellEditorInitialized event which is fired when the initialization of an editor is done in order to access the editor:

GridViewDateTimeColumn dateTimeColumn1 = new GridViewDateTimeColumn();
dateTimeColumn1.Name = "DateTimeColumn";
dateTimeColumn1.HeaderText = "Order date";
dateTimeColumn1.FieldName = "OrderDate";
dateTimeColumn1.Format = DateTimePickerFormat.Custom;
dateTimeColumn1.CustomFormat = "t";

Private Sub radGridView1_CellEditorInitialized(ByVal sender As Object, ByVal e As GridViewCellEventArgs)
    Dim editor As RadDateTimeEditor = TryCast(Me.RadGridView1.ActiveEditor, RadDateTimeEditor)
    If editor IsNot Nothing Then
        'Pick up one of the default formats
        DirectCast(DirectCast(Me.RadGridView1.ActiveEditor, RadDateTimeEditor).EditorElement, RadDateTimeEditorElement).Format = DateTimePickerFormat.[Short]
        'Or set a custom date format
        DirectCast(DirectCast(Me.RadGridView1.ActiveEditor, RadDateTimeEditor).EditorElement, RadDateTimeEditorElement).CustomFormat = "t"
    End If
End Sub

void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
{
    RadDateTimeEditor editor = this.radGridView1.ActiveEditor as RadDateTimeEditor;
    if (editor != null)
    {   
        //Pick up one of the default formats
        ((RadDateTimeEditorElement)((RadDateTimeEditor)this.radGridView1.ActiveEditor).EditorElement).Format = DateTimePickerFormat.Short;

        //Or set a custom date format
        ((RadDateTimeEditorElement)((RadDateTimeEditor)this.radGridView1.ActiveEditor).EditorElement).CustomFormat = "t";
    }
}

Private Sub radGridView1_CellEditorInitialized(ByVal sender As Object, ByVal e As GridViewCellEventArgs)
    Dim editor As RadDateTimeEditor = TryCast(Me.RadGridView1.ActiveEditor, RadDateTimeEditor)
    If editor IsNot Nothing Then
        'Pick up one of the default formats
        DirectCast(DirectCast(Me.RadGridView1.ActiveEditor, RadDateTimeEditor).EditorElement, RadDateTimeEditorElement).Format = DateTimePickerFormat.[Short]
        'Or set a custom date format
        DirectCast(DirectCast(Me.RadGridView1.ActiveEditor, RadDateTimeEditor).EditorElement, RadDateTimeEditorElement).CustomFormat = "t"
    End If
End Sub

If we do not use the CellEditorInitialized, but CellBeginEdit (it is fired before CellEditorInitialized), our Format setting will be overridden by the initialization of the editor.

You can also change the way the dates in the column are filtered. This is how the column can be adjusted to filter only by Dates.

dateTimeColumn1.FilteringMode = GridViewTimeFilteringMode.Date;

dateTimeColumn1.FilteringMode = GridViewTimeFilteringMode.Date

DateTimeKind property

This property allows you to transform the date/time values from database to the local time. By default RadGridView supposes that the date/time values are stored in UTC in the database.

Member Name Description
Local If Local is assigned to a column, in this mode, its values will first converted into Local time.
Unspecified Value will not converted.
Utc Value will not converted.

EditorType

The EditorType allows you to easily change the editor type. It could be set to three values:

  • DateTimePicker: this is the default value and the editor will be a standard RadDateTimePicker.

  • TimePicker: the editor will be a RadTimePicker.

  • DateTimePickerSpinMode: this type of editor shows up/down buttons instead of a drop down with a calendar.

See Also

In this article