Skip to content

Commit

Permalink
Merge pull request #2994 from telerik/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
martinivanoff authored Dec 9, 2024
2 parents c35a2cf + e6db4a8 commit aebf67e
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 15 deletions.
64 changes: 49 additions & 15 deletions controls/radgridview/export/how-to/export-datetime.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Export DateTime Value
page_title: Export DateTime Value
description: Read this article to learn how to export DateTime values from Telerik's {{ site.framework_name }} DataGrid without having the values appear as numbers.
description: Read this article to learn how to export DateTime values from Telerik's DataGrid without having the values appear as numbers.
slug: gridview-export-datetime
tags: export, datetime, value
published: True
Expand All @@ -12,30 +12,31 @@ position: 5

__PROBLEM__

1) When exporting DateTime values without a specified **DataFormatString** to Excel, the values appear as numbers as shown in **Figure 1**:
1) When exporting DateTime values without a specified __DataFormatString__ to Excel, the values appear as numbers as shown in the below image:

#### __Figure 1: Exporting DateTime values without specified DataFormatString__
__Exporting DateTime values without specified DataFormatString__

![Telerik {{ site.framework_name }} DataGrid-export-datetime-as-number](images/gridview-export-datetime-as-number.png)
![Telerik DataGrid-export-datetime-as-number](images/gridview-export-datetime-as-number.png)

2) When exporting DateTime values with specified **DataFormatString** to Excel, the values appear as strings as shown in **Figure 2**:
2) When exporting DateTime values with specified __DataFormatString__ to Excel, the values appear as strings as shown in the next image:

#### __Figure 2: Exporting DateTime values with specified DataFormatString__
__Exporting DateTime values with specified DataFormatString__

![Telerik {{ site.framework_name }} DataGrid-export-datetime-as-string](images/gridview-export-datetime-as-string.png)
![Telerik DataGrid-export-datetime-as-string](images/gridview-export-datetime-as-string.png)

__CAUSE__

1) In most modern programming environments, dates are stored as real numbers. The integer part of the number is the number of days since some agreed-upon date in the past, called the epoch. In Excel, June 16, 2006, for example, is stored as 38884, counting days where January 1st, 1900 is 1.

2) When a DataFormatString has been specified for a given column, RadGridView exports the string representation of the values in that column.

__SOLUTION__
__SOLUTIONs__

When an element is exported through the [ExportToXlsx]({%slug gridview-export-xlsx%}), [ExportToPdf]({%slug gridview-export-pdf%}), [ExportToWorkbook]({%slug gridview-export-workbook%}) or [SpreadsheetStreamingExport]({%slug gridview-export-spreadsheetstreamingexport%}) methods, the arguments of the [ElementExportingToDocument]({%slug gridview-export-events-elementexporting-elementexported-todocument%}) event can be used to modify the visual appearance of the exported values and specify how they should be [formatted](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/features/format-codes#date-and-time-formatting) in Excel. This is achieved through the **VisualParameters** property of the **GridViewCellExportingEventArgs**.
## Export via the Built-In RadGridView Export Methods

#### __[C#] Example 1: Exporting DateTime Values to Excel__
When an element is exported through the [ExportToXlsx]({%slug gridview-export-xlsx%}), [ExportToPdf]({%slug gridview-export-pdf%}) or [ExportToWorkbook]({%slug gridview-export-workbook%}) or methods, the arguments of the [ElementExportingToDocument]({%slug gridview-export-events-elementexporting-elementexported-todocument%}) event can be used to modify the visual appearance of the exported values and specify how they should be [formatted](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/features/format-codes#date-and-time-formatting) in Excel. This is achieved through the `VisualParameters` property of the `GridViewCellExportingEventArgs`.

#### __[C#] Exporting DateTime Values to Excel__
{{region cs-gridview-export-how-to-export-datetime-1}}
this.radGridView.ElementExportingToDocument += (s, e) =>
{
Expand All @@ -54,8 +55,7 @@ When an element is exported through the [ExportToXlsx]({%slug gridview-export-xl
};
{{endregion}}

#### __[VB.NET] Example 1: Exporting DateTime Values to Excel__

#### __[VB.NET] Exporting DateTime Values to Excel__
{{region vb-gridview-export-how-to-export-datetime-2}}
AddHandler Me.radGridView.ElementExportingToDocument, Sub(s, e)
If e.Element = ExportElement.Cell Then
Expand All @@ -65,12 +65,46 @@ When an element is exported through the [ExportToXlsx]({%slug gridview-export-xl
parameters.Style = New CellSelectionStyle() With {.Format = New CellValueFormat("m/d/yyyy")}
End If
End If
End Sub
End Sub
{{endregion}}

#### __Figure 3: Exporting DateTime values with ElementExportingToDocument__
__Exporting DateTime values with ElementExportingToDocument__

![Telerik DataGrid-export-datetime-as-datetime](images/gridview-export-datetime-as-datetime.png)

## Export via the GridViewSpreadStreamExport Class

![Telerik {{ site.framework_name }} DataGrid-export-datetime-as-datetime](images/gridview-export-datetime-as-datetime.png)
When exporting the RadGridView with the [GridViewSpreadStreamExport class]({%slug gridview-export-spreadsheetstreamingexport%}), the event arguments of `ElementExportingToDocument` event will be of the type of `GridViewSpreadStreamElementExportingEventArgs`. To format the number value, create a new `SpreadCellFormat` instance and set the `NumberFormat` property. To apply the formatting, create a new SpreadStreamCellStyle instance, set the created `SpreadCellFormat` to its `CellFormat` property, and apply it to the `e.Style` property of the event arguments.

#### __[C#] Specify a format when exporting with the GridViewSpreadStreamExport class__
{{region gridview-export-how-to-export-datetime-3}}
private static void SpreadStreamExport_ElementExportingToDocument(object sender, GridViewSpreadStreamElementExportingEventArgs e)
{
if (e.Element == SpreadStreamExportElement.Cell && e.Value is DateTime)
{
e.Style = new SpreadStreamCellStyle()
{
CellFormat = new SpreadCellFormat()
{
NumberFormat = BuiltInNumberFormats.GetDayMonthLongYear() + " " + BuiltInNumberFormats.GetHourMinuteSecondAMPM()
}
};
}
}
{{endregion}}

#### __[VB.NET] Specify a format when exporting with the GridViewSpreadStreamExport class__
{{region gridview-export-how-to-export-datetime-4}}
Private Shared Sub SpreadStreamExport_ElementExportingToDocument(ByVal sender As Object, ByVal e As GridViewSpreadStreamElementExportingEventArgs)
If e.Element = SpreadStreamExportElement.Cell AndAlso TypeOf e.Value Is DateTime Then
e.Style = New SpreadStreamCellStyle() With {
.CellFormat = New SpreadCellFormat() With {
.NumberFormat = BuiltInNumberFormats.GetDayMonthLongYear() & " " + BuiltInNumberFormats.GetHourMinuteSecondAMPM()
}
}
End If
End Sub
{{endregion}}

## See Also

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
---
title: Allowed Types
page_title: Allowed Types
description: Register types that are allowed to be instantiated by the deserialization mechanism of PersistenceFramework.
slug: persistence-framework-allowed-types
tags: allowed types, types, persistence framework
published: True
position: 8
---

# Allowed Types

When loading the persisted layout, the `PersistenceFramework` will instantiate the saved types only if they are allowed. This is controlled by the `PersistenceManager` class, which has predefined types that are allowed by default. You can also register other types by modifying its `AllowedTypes` collection or by using the `AllowTypes` extension method.

#### __[C#] Allowing a type using the AllowedTypes collection of the PersistenceManager__
{{region persistence-framework-allowed-types-0}}
PersistenceManager manager = new PersistenceManager();
manager.AllowedTypes.Add(typeof(SolidColorBrush));
{{endregion}}

#### __[VB.NET] Allowing a type using the AllowedTypes collection of the PersistenceManager__
{{region persistence-framework-allowed-types-1}}
Dim manager As PersistenceManager = New PersistenceManager()
manager.AllowedTypes.Add(GetType(SolidColorBrush))
{{endregion}}

#### __[C#] Allowing a type using the AllowTypes extension method__
{{region persistence-framework-allowed-types-2}}
PersistenceManager manager = new PersistenceManager().AllowTypes(typeof(SolidColorBrush));
{{endregion}}

#### __[VB.NET] Allowing a type using the AllowTypes extension method__
{{region persistence-framework-allowed-types-3}}
Dim manager As PersistenceManager = New PersistenceManager().AllowTypes(GetType(SolidColorBrush))
{{endregion}}

## Allowing Types from the Telerik Assemblies

Each Telerik assembly has an extension method that will load the types in the AllowedTypes collection of a control, on which it is invoked. In the scope of the PersistenceFramework, calling these methods on the PersistenceManager will update its AllowedTypes collection.

The following example will show you how to allow the types that are used in the __Telerik.Windows.Controls.Docking.dll__ and __Telerik.Windows.Controls.Navigation.dll__ inside the PersistenceManager.

#### __[C#] Allowing the types that are used in the Telerik.Windows.Controls.Docking and Telerik.Windows.Controls.Navigation assemblies__
{{region persistence-framework-allowed-types-4}}
PersistenceManager manager = new PersistenceManager()
.AllowDockingControls()
.AllowNavigationControls();
{{endregion}}

#### __[VB.NET] Allowing the types that are used in the Telerik.Windows.Controls.Docking and Telerik.Windows.Controls.Navigation assemblies__
{{region persistence-framework-allowed-types-5}}
Dim manager As PersistenceManager = New PersistenceManager()
.AllowDockingControls()
.AllowNavigationControls()
{{endregion}}

## Allowing Internal Types

In cases where internal WPF types need to be added to the AllowedTypes collection, you can obtain them at runtime and utilize the AllowTypes method. One such case is with the native WPF `Selector` class that assigns the `SelectedItems` property to a `SelectedItemCollection` type, which is internal. In this specific scenario, invoking the `AllowInputControls` method will resolve this, however, if you need to allow internal types, you can follow the approach from the following example:

#### __[C#] Allowing internal types via the AllowTypes method__
{{region persistence-framework-allowed-types-6}}
var listBox = new ListBox();
manager.AllowTypes(listBox.SelectedItems.GetType());
{{endregion}}

#### __[VB.NET] Allowing internal types via the AllowTypes method__
{{region persistence-framework-allowed-types-7}}
Dim listBox = New ListBox()
manager.AllowTypes(listBox.SelectedItems.[GetType]())
{{endregion}}

## TypeRestored Event

To retrieve each type that is present in the saved layout, you can utilize the `TypeRestored` event. You can inspect both the type and its assembly, and decide whether to allow the PersistenceFramework to instantiate it, by adding it to the AllowedTypes collection. The event arguments are of the type of `TypeRestoredEventArgs` and provide the following API:

* `Type`—Provides the type that is restored.
* `AssemblyQualifiedName`—Provides the type's assembly name.

#### __[C#] Utilizing the TypeRestored event__
{{region persistence-framework-allowed-types-8}}
private void Manager_TypeRestored(object sender, Persistence.Events.TypeRestoredEventArgs e)
{
var type = e.Type; // Review the type and decide whether to allow the PersistenceFramework to instantiate it by adding it to the AllowedTypes collection.
}
{{endregion}}

#### __[VB.NET] Utilizing the TypeRestored event__
{{region persistence-framework-allowed-types-9}}
Private Sub Manager_TypeRestored(sender As Object, e As Persistence.Events.TypeRestoredEventArgs)
Dim type = e.Type ' Review the type and decide whether to allow the PersistenceFramework to instantiate it by adding it to the AllowedTypes collection.
End Sub
{{endregion}}

## Allowing Types when Using IsolatedStorageProvider

To allow types to be instantiated by the PersistenceFramework when working with the `IsolatedStorageProvider`, you need to pass a new PersistenceManager instance to its constructor. On it, you can utilize the AllowTypes extension method or use its AllowedTypes collection.

#### __[C#] Allowing types when using IsolatedStorageProvider__
{{region persistence-framework-allowed-types-10}}
PersistenceManager manager = new PersistenceManager()
.AddDockingControls()
.AddNavigationControls();

IsolatedStorageProvider isoProvider = new IsolatedStorageProvider(manager);
{{endregion}}

#### __[VB.NET] Allowing types when using IsolatedStorageProvider__
{{region persistence-framework-allowed-types-11}}
Dim manager As PersistenceManager = New PersistenceManager()
.AddDockingControls()
.AddNavigationControls()

Dim isoProvider As IsolatedStorageProvider = New IsolatedStorageProvider(manager)
{{endregion}}

0 comments on commit aebf67e

Please sign in to comment.