Skip to content

Commit aebf67e

Browse files
Merge pull request #2994 from telerik/development
Development
2 parents c35a2cf + e6db4a8 commit aebf67e

File tree

2 files changed

+164
-15
lines changed

2 files changed

+164
-15
lines changed

controls/radgridview/export/how-to/export-datetime.md

+49-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Export DateTime Value
33
page_title: Export DateTime Value
4-
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.
4+
description: Read this article to learn how to export DateTime values from Telerik's DataGrid without having the values appear as numbers.
55
slug: gridview-export-datetime
66
tags: export, datetime, value
77
published: True
@@ -12,30 +12,31 @@ position: 5
1212

1313
__PROBLEM__
1414

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

17-
#### __Figure 1: Exporting DateTime values without specified DataFormatString__
17+
__Exporting DateTime values without specified DataFormatString__
1818

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

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

23-
#### __Figure 2: Exporting DateTime values with specified DataFormatString__
23+
__Exporting DateTime values with specified DataFormatString__
2424

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

2727
__CAUSE__
2828

2929
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.
3030

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

33-
__SOLUTION__
33+
__SOLUTIONs__
3434

35-
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**.
35+
## Export via the Built-In RadGridView Export Methods
3636

37-
#### __[C#] Example 1: Exporting DateTime Values to Excel__
37+
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`.
3838

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

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

71-
#### __Figure 3: Exporting DateTime values with ElementExportingToDocument__
71+
__Exporting DateTime values with ElementExportingToDocument__
72+
73+
![Telerik DataGrid-export-datetime-as-datetime](images/gridview-export-datetime-as-datetime.png)
74+
75+
## Export via the GridViewSpreadStreamExport Class
7276

73-
![Telerik {{ site.framework_name }} DataGrid-export-datetime-as-datetime](images/gridview-export-datetime-as-datetime.png)
77+
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.
78+
79+
#### __[C#] Specify a format when exporting with the GridViewSpreadStreamExport class__
80+
{{region gridview-export-how-to-export-datetime-3}}
81+
private static void SpreadStreamExport_ElementExportingToDocument(object sender, GridViewSpreadStreamElementExportingEventArgs e)
82+
{
83+
if (e.Element == SpreadStreamExportElement.Cell && e.Value is DateTime)
84+
{
85+
e.Style = new SpreadStreamCellStyle()
86+
{
87+
CellFormat = new SpreadCellFormat()
88+
{
89+
NumberFormat = BuiltInNumberFormats.GetDayMonthLongYear() + " " + BuiltInNumberFormats.GetHourMinuteSecondAMPM()
90+
}
91+
};
92+
}
93+
}
94+
{{endregion}}
95+
96+
#### __[VB.NET] Specify a format when exporting with the GridViewSpreadStreamExport class__
97+
{{region gridview-export-how-to-export-datetime-4}}
98+
Private Shared Sub SpreadStreamExport_ElementExportingToDocument(ByVal sender As Object, ByVal e As GridViewSpreadStreamElementExportingEventArgs)
99+
If e.Element = SpreadStreamExportElement.Cell AndAlso TypeOf e.Value Is DateTime Then
100+
e.Style = New SpreadStreamCellStyle() With {
101+
.CellFormat = New SpreadCellFormat() With {
102+
.NumberFormat = BuiltInNumberFormats.GetDayMonthLongYear() & " " + BuiltInNumberFormats.GetHourMinuteSecondAMPM()
103+
}
104+
}
105+
End If
106+
End Sub
107+
{{endregion}}
74108

75109
## See Also
76110

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
title: Allowed Types
3+
page_title: Allowed Types
4+
description: Register types that are allowed to be instantiated by the deserialization mechanism of PersistenceFramework.
5+
slug: persistence-framework-allowed-types
6+
tags: allowed types, types, persistence framework
7+
published: True
8+
position: 8
9+
---
10+
11+
# Allowed Types
12+
13+
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.
14+
15+
#### __[C#] Allowing a type using the AllowedTypes collection of the PersistenceManager__
16+
{{region persistence-framework-allowed-types-0}}
17+
PersistenceManager manager = new PersistenceManager();
18+
manager.AllowedTypes.Add(typeof(SolidColorBrush));
19+
{{endregion}}
20+
21+
#### __[VB.NET] Allowing a type using the AllowedTypes collection of the PersistenceManager__
22+
{{region persistence-framework-allowed-types-1}}
23+
Dim manager As PersistenceManager = New PersistenceManager()
24+
manager.AllowedTypes.Add(GetType(SolidColorBrush))
25+
{{endregion}}
26+
27+
#### __[C#] Allowing a type using the AllowTypes extension method__
28+
{{region persistence-framework-allowed-types-2}}
29+
PersistenceManager manager = new PersistenceManager().AllowTypes(typeof(SolidColorBrush));
30+
{{endregion}}
31+
32+
#### __[VB.NET] Allowing a type using the AllowTypes extension method__
33+
{{region persistence-framework-allowed-types-3}}
34+
Dim manager As PersistenceManager = New PersistenceManager().AllowTypes(GetType(SolidColorBrush))
35+
{{endregion}}
36+
37+
## Allowing Types from the Telerik Assemblies
38+
39+
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.
40+
41+
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.
42+
43+
#### __[C#] Allowing the types that are used in the Telerik.Windows.Controls.Docking and Telerik.Windows.Controls.Navigation assemblies__
44+
{{region persistence-framework-allowed-types-4}}
45+
PersistenceManager manager = new PersistenceManager()
46+
.AllowDockingControls()
47+
.AllowNavigationControls();
48+
{{endregion}}
49+
50+
#### __[VB.NET] Allowing the types that are used in the Telerik.Windows.Controls.Docking and Telerik.Windows.Controls.Navigation assemblies__
51+
{{region persistence-framework-allowed-types-5}}
52+
Dim manager As PersistenceManager = New PersistenceManager()
53+
.AllowDockingControls()
54+
.AllowNavigationControls()
55+
{{endregion}}
56+
57+
## Allowing Internal Types
58+
59+
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:
60+
61+
#### __[C#] Allowing internal types via the AllowTypes method__
62+
{{region persistence-framework-allowed-types-6}}
63+
var listBox = new ListBox();
64+
manager.AllowTypes(listBox.SelectedItems.GetType());
65+
{{endregion}}
66+
67+
#### __[VB.NET] Allowing internal types via the AllowTypes method__
68+
{{region persistence-framework-allowed-types-7}}
69+
Dim listBox = New ListBox()
70+
manager.AllowTypes(listBox.SelectedItems.[GetType]())
71+
{{endregion}}
72+
73+
## TypeRestored Event
74+
75+
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:
76+
77+
* `Type`—Provides the type that is restored.
78+
* `AssemblyQualifiedName`—Provides the type's assembly name.
79+
80+
#### __[C#] Utilizing the TypeRestored event__
81+
{{region persistence-framework-allowed-types-8}}
82+
private void Manager_TypeRestored(object sender, Persistence.Events.TypeRestoredEventArgs e)
83+
{
84+
var type = e.Type; // Review the type and decide whether to allow the PersistenceFramework to instantiate it by adding it to the AllowedTypes collection.
85+
}
86+
{{endregion}}
87+
88+
#### __[VB.NET] Utilizing the TypeRestored event__
89+
{{region persistence-framework-allowed-types-9}}
90+
Private Sub Manager_TypeRestored(sender As Object, e As Persistence.Events.TypeRestoredEventArgs)
91+
Dim type = e.Type ' Review the type and decide whether to allow the PersistenceFramework to instantiate it by adding it to the AllowedTypes collection.
92+
End Sub
93+
{{endregion}}
94+
95+
## Allowing Types when Using IsolatedStorageProvider
96+
97+
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.
98+
99+
#### __[C#] Allowing types when using IsolatedStorageProvider__
100+
{{region persistence-framework-allowed-types-10}}
101+
PersistenceManager manager = new PersistenceManager()
102+
.AddDockingControls()
103+
.AddNavigationControls();
104+
105+
IsolatedStorageProvider isoProvider = new IsolatedStorageProvider(manager);
106+
{{endregion}}
107+
108+
#### __[VB.NET] Allowing types when using IsolatedStorageProvider__
109+
{{region persistence-framework-allowed-types-11}}
110+
Dim manager As PersistenceManager = New PersistenceManager()
111+
.AddDockingControls()
112+
.AddNavigationControls()
113+
114+
Dim isoProvider As IsolatedStorageProvider = New IsolatedStorageProvider(manager)
115+
{{endregion}}

0 commit comments

Comments
 (0)