Skip to content

Commit 5db706a

Browse files
Merge pull request #1 from SyncfusionExamples/customColumnSr
Code changes regarding serialize custom column in dataGrid
2 parents 0a1e456 + e3e400a commit 5db706a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+9673
-0
lines changed

SfDataGridSample/SfDataGridSample.sln

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35506.116
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SfDataGridSample", "SfDataGridSample\SfDataGridSample.csproj", "{EEB0310E-49AF-4663-87C2-E585EEFE9E5E}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{EEB0310E-49AF-4663-87C2-E585EEFE9E5E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{EEB0310E-49AF-4663-87C2-E585EEFE9E5E}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{EEB0310E-49AF-4663-87C2-E585EEFE9E5E}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{EEB0310E-49AF-4663-87C2-E585EEFE9E5E}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version = "1.0" encoding = "UTF-8" ?>
2+
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:local="clr-namespace:SfDataGridSample"
5+
x:Class="SfDataGridSample.App">
6+
<Application.Resources>
7+
<ResourceDictionary>
8+
<ResourceDictionary.MergedDictionaries>
9+
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
10+
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
11+
</ResourceDictionary.MergedDictionaries>
12+
</ResourceDictionary>
13+
</Application.Resources>
14+
</Application>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace SfDataGridSample
2+
{
3+
public partial class App : Application
4+
{
5+
public App()
6+
{
7+
InitializeComponent();
8+
}
9+
10+
protected override Window CreateWindow(IActivationState? activationState)
11+
{
12+
return new Window(new AppShell());
13+
}
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<Shell
3+
x:Class="SfDataGridSample.AppShell"
4+
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6+
xmlns:local="clr-namespace:SfDataGridSample"
7+
Shell.FlyoutBehavior="Flyout"
8+
Title="SfDataGridSample">
9+
10+
<ShellContent
11+
Title="Home"
12+
ContentTemplate="{DataTemplate local:MainPage}"
13+
Route="MainPage" />
14+
15+
</Shell>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace SfDataGridSample
2+
{
3+
public partial class AppShell : Shell
4+
{
5+
public AppShell()
6+
{
7+
InitializeComponent();
8+
}
9+
}
10+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:syncfusion="clr-namespace:Syncfusion.Maui.DataGrid;assembly=Syncfusion.Maui.DataGrid"
5+
xmlns:local="clr-namespace:SfDataGridSample"
6+
x:Class="SfDataGridSample.MainPage">
7+
8+
<ContentPage.BindingContext>
9+
<local:EmployeeViewModel x:Name="viewModel" />
10+
</ContentPage.BindingContext>
11+
12+
<Grid ColumnDefinitions="*,300"
13+
RowDefinitions="Auto,Auto">
14+
<syncfusion:SfDataGrid x:Name="dataGrid"
15+
Grid.Column="0"
16+
Grid.Row="0"
17+
AllowGroupExpandCollapse="True"
18+
ColumnWidthMode="Auto"
19+
GridLinesVisibility="Both"
20+
HeaderGridLinesVisibility="Both"
21+
AutoGenerateColumnsMode="None"
22+
ItemsSource="{Binding Employees}">
23+
24+
<syncfusion:SfDataGrid.Columns>
25+
<syncfusion:DataGridNumericColumn MappingName="EmployeeID"
26+
HeaderText="Employee ID" />
27+
<syncfusion:DataGridTextColumn MappingName="Title"
28+
HeaderText="Designation" />
29+
<syncfusion:DataGridDateColumn MappingName="HireDate"
30+
HeaderText="Hire Date" />
31+
<local:TextImageColumn MappingName="Title" />
32+
33+
</syncfusion:SfDataGrid.Columns>
34+
</syncfusion:SfDataGrid>
35+
36+
<syncfusion:SfDataGrid x:Name="dataGrid1"
37+
Grid.Column="0"
38+
Grid.Row="1"
39+
ItemsSource="{Binding Employees}">
40+
</syncfusion:SfDataGrid>
41+
42+
<VerticalStackLayout Grid.Column="1">
43+
<Grid RowDefinitions="80,80">
44+
<Button Text="Serialize"
45+
Grid.Row="0"
46+
Margin="0,0,0,20"
47+
WidthRequest="150"
48+
Clicked="Button_Clicked" />
49+
<Button Text="Deserialize"
50+
Grid.Row="1"
51+
Margin="0,20,0,0"
52+
WidthRequest="150"
53+
Clicked="Button_Clicked_1" />
54+
</Grid>
55+
</VerticalStackLayout>
56+
</Grid>
57+
58+
</ContentPage>
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
using Syncfusion.Maui.DataGrid;
2+
using static SfDataGridSample.TextImageColumn;
3+
using System.Globalization;
4+
using System.Reflection;
5+
using System.Runtime.Serialization;
6+
7+
namespace SfDataGridSample
8+
{
9+
public partial class MainPage : ContentPage
10+
{
11+
public MainPage()
12+
{
13+
InitializeComponent();
14+
dataGrid.CellRenderers.Add("TextImage", new TextImageColumnRenderer());
15+
dataGrid1.CellRenderers.Add("TextImage", new TextImageColumnRenderer());
16+
dataGrid.SerializationController = new SerializationControllerCustomExt(dataGrid);
17+
dataGrid1.SerializationController = new SerializationControllerCustomExt(dataGrid1);
18+
}
19+
20+
private void Button_Clicked(object sender, EventArgs e)
21+
{
22+
string localPath = Path.Combine(FileSystem.AppDataDirectory, "DataGrid.xml");
23+
using (var file = File.Create(localPath))
24+
{
25+
dataGrid.Serialize(file);
26+
}
27+
}
28+
29+
private void Button_Clicked_1(object sender, EventArgs e)
30+
{
31+
string localPath = Path.Combine(FileSystem.AppDataDirectory, "DataGrid.xml");
32+
33+
using (var file = File.Open(localPath, FileMode.Open))
34+
{
35+
dataGrid1.Deserialize(file);
36+
}
37+
}
38+
}
39+
40+
[DataContract(Name = "SerializableCustomGridColumn")]
41+
public class SerializableCustomGridColumn : SerializableDataGridColumn
42+
{
43+
[DataMember]
44+
public string? DateMappingName { get; set; }
45+
}
46+
47+
public class SerializationControllerCustomExt : DataGridSerializationController
48+
{
49+
public SerializationControllerCustomExt(SfDataGrid sfGrid) : base(sfGrid) { }
50+
51+
protected override SerializableDataGridColumn GetSerializableColumn(DataGridColumn column)
52+
{
53+
if (column is TextImageColumn)
54+
{
55+
return new SerializableCustomGridColumn();
56+
}
57+
return base.GetSerializableColumn(column);
58+
}
59+
60+
protected override void StoreColumnProperties(DataGridColumn column, SerializableDataGridColumn serializableColumn)
61+
{
62+
base.StoreColumnProperties(column, serializableColumn);
63+
64+
if (column is TextImageColumn textImageColumn && serializableColumn is SerializableCustomGridColumn customColumn)
65+
{
66+
customColumn.DateMappingName = textImageColumn.MappingName;
67+
}
68+
}
69+
70+
protected override DataGridColumn GetColumn(SerializableDataGridColumn serializableColumn)
71+
{
72+
if (serializableColumn is SerializableCustomGridColumn)
73+
{
74+
return new TextImageColumn();
75+
}
76+
return base.GetColumn(serializableColumn);
77+
}
78+
79+
protected override void RestoreColumnProperties(SerializableDataGridColumn serializableColumn, DataGridColumn column)
80+
{
81+
base.RestoreColumnProperties(serializableColumn, column);
82+
83+
if (column is TextImageColumn textImageColumn && serializableColumn is SerializableCustomGridColumn customColumn)
84+
{
85+
if (!string.IsNullOrEmpty(customColumn.DateMappingName))
86+
textImageColumn.MappingName = customColumn.DateMappingName;
87+
}
88+
}
89+
90+
public override Type[] KnownTypes()
91+
{
92+
var types = base.KnownTypes().ToList();
93+
types.Add(typeof(SerializableCustomGridColumn));
94+
return types.ToArray();
95+
}
96+
97+
}
98+
99+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Microsoft.Extensions.Logging;
2+
using Syncfusion.Maui.Core.Hosting;
3+
4+
namespace SfDataGridSample
5+
{
6+
public static class MauiProgram
7+
{
8+
public static MauiApp CreateMauiApp()
9+
{
10+
var builder = MauiApp.CreateBuilder();
11+
builder
12+
.UseMauiApp<App>()
13+
.ConfigureSyncfusionCore()
14+
.ConfigureFonts(fonts =>
15+
{
16+
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
17+
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
18+
});
19+
20+
#if DEBUG
21+
builder.Logging.AddDebug();
22+
#endif
23+
24+
return builder.Build();
25+
}
26+
}
27+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
using System.ComponentModel;
2+
3+
namespace SfDataGridSample
4+
{
5+
public class Employee : INotifyPropertyChanged
6+
{
7+
private int? _employeeID;
8+
private string? _name;
9+
private long _iDNumber;
10+
private int _contactID;
11+
private string? _loginID;
12+
private int _managerID;
13+
private string? _title;
14+
private DateTime _birthDate;
15+
private string? _maritalStatus;
16+
private string? _gender;
17+
private DateTime _hireDate;
18+
private int _sickLeaveHours;
19+
private double _salary;
20+
private bool _employeeStatus;
21+
private int _rating;
22+
private bool _isButtonVisible;
23+
24+
public int? EmployeeID
25+
{
26+
get { return _employeeID; }
27+
set { _employeeID = value; OnPropertyChanged(nameof(EmployeeID)); }
28+
}
29+
public string? Name
30+
{
31+
get { return _name; }
32+
set { _name = value; OnPropertyChanged(nameof(Name)); }
33+
}
34+
public long IDNumber
35+
{
36+
get { return _iDNumber; }
37+
set { _iDNumber = value; OnPropertyChanged(nameof(IDNumber)); }
38+
}
39+
public string? Title
40+
{
41+
get { return _title; }
42+
set { _title = value; OnPropertyChanged(nameof(Title)); }
43+
}
44+
public int ContactID
45+
{
46+
get { return _contactID; }
47+
set { _contactID = value; OnPropertyChanged(nameof(ContactID)); }
48+
}
49+
public DateTime BirthDate
50+
{
51+
get { return _birthDate; }
52+
set { _birthDate = value; OnPropertyChanged(nameof(BirthDate)); }
53+
}
54+
public string? MaritalStatus
55+
{
56+
get { return _maritalStatus; }
57+
set { _maritalStatus = value; OnPropertyChanged(nameof(MaritalStatus)); }
58+
}
59+
public string? Gender
60+
{
61+
get { return _gender; }
62+
set { _gender = value; OnPropertyChanged(nameof(Gender)); }
63+
}
64+
public DateTime HireDate
65+
{
66+
get { return _hireDate; }
67+
set { _hireDate = value; OnPropertyChanged(nameof(HireDate)); }
68+
}
69+
public int SickLeaveHours
70+
{
71+
get { return _sickLeaveHours; }
72+
set { _sickLeaveHours = value; OnPropertyChanged(nameof(SickLeaveHours)); }
73+
}
74+
public double Salary
75+
{
76+
get { return _salary; }
77+
set { _salary = value; OnPropertyChanged(nameof(Salary)); }
78+
}
79+
public string? LoginID
80+
{
81+
get { return _loginID; }
82+
set { _loginID = value; OnPropertyChanged(nameof(LoginID)); }
83+
}
84+
public int ManagerID
85+
{
86+
get { return _managerID; }
87+
set { _managerID = value; OnPropertyChanged(nameof(ManagerID)); }
88+
}
89+
public bool EmployeeStatus
90+
{
91+
get { return _employeeStatus; }
92+
set { _employeeStatus = value; OnPropertyChanged(nameof(EmployeeStatus)); }
93+
}
94+
public int Rating
95+
{
96+
get { return _rating; }
97+
set { _rating = value; OnPropertyChanged(nameof(Rating)); }
98+
}
99+
100+
public bool IsButtonVisible
101+
{
102+
get { return _isButtonVisible; }
103+
set { _isButtonVisible = value; OnPropertyChanged(nameof(IsButtonVisible)); }
104+
}
105+
106+
public event PropertyChangedEventHandler? PropertyChanged;
107+
108+
protected void OnPropertyChanged(string propertyName)
109+
{
110+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
111+
}
112+
}
113+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
3+
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
4+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
</manifest>

0 commit comments

Comments
 (0)