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
kclicks on the cell to invoke the editor.
Copy[C#] Instantiate GridViewDateTimeColumn
GridViewDateTimeColumn dateTimeColumn = new GridViewDateTimeColumn();
dateTimeColumn.Name = "DateTimeColumn";
dateTimeColumn.HeaderText = "Order date";
dateTimeColumn.FieldName = "OrderDate";
dateTimeColumn.FormatString = "{0:D}";
Copy[VB.NET] Instantiate GridViewDateTimeColumn
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 as
demonstrated in the example below where CultureInfo is set to French-Belgium.
Copy[C#] Set the CurrentCulture
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);
Copy[VB.NET] Set the CurrentCulture
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:
Copy[C#] Change the date format
GridViewDateTimeColumn dateTimeColumn1 = new GridViewDateTimeColumn();
dateTimeColumn1.Name = "DateTimeColumn";
dateTimeColumn1.HeaderText = "Order date";
dateTimeColumn1.FieldName = "OrderDate";
dateTimeColumn1.Format = DateTimePickerFormat.Custom;
dateTimeColumn1.CustomFormat = "t";
Copy[VB.NET] Change the date format
Dim dateTimeColumn1 As New GridViewDateTimeColumn()
dateTimeColumn1.Name = "DateTimeColumn"
dateTimeColumn1.HeaderText = "Order date"
dateTimeColumn1.FieldName = "OrderDate"
dateTimeColumn1.Format = DateTimePickerFormat.Custom
dateTimeColumn1.CustomFormat = "t"
Copy[C#] Change the date format
void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
{
RadDateTimeEditor editor = this.radGridView1.ActiveEditor as RadDateTimeEditor;
if (editor != null)
{
((RadDateTimeEditorElement)((RadDateTimeEditor)this.radGridView1.ActiveEditor).EditorElement).Format = DateTimePickerFormat.Short;
((RadDateTimeEditorElement)((RadDateTimeEditor)this.radGridView1.ActiveEditor).EditorElement).CustomFormat = "t";
}
}
Copy[VB.NET] Change the date format
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
DirectCast(DirectCast(Me.RadGridView1.ActiveEditor, RadDateTimeEditor).EditorElement, RadDateTimeEditorElement).Format = DateTimePickerFormat.[Short]
DirectCast(DirectCast(Me.RadGridView1.ActiveEditor, RadDateTimeEditor).EditorElement, RadDateTimeEditorElement).CustomFormat = "t"
End If
End SubIf we do not use the CellEditorInitialized, but CellBeginEdit (CellBeginEdit is fired before CellEditorInitialized), our Format setting will be overriden by the initialization of the editor.