Skip to content

Code changes regarding serialize custom column in dataGrid #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions SfDataGridSample/SfDataGridSample.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35506.116
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SfDataGridSample", "SfDataGridSample\SfDataGridSample.csproj", "{EEB0310E-49AF-4663-87C2-E585EEFE9E5E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{EEB0310E-49AF-4663-87C2-E585EEFE9E5E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EEB0310E-49AF-4663-87C2-E585EEFE9E5E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EEB0310E-49AF-4663-87C2-E585EEFE9E5E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EEB0310E-49AF-4663-87C2-E585EEFE9E5E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
14 changes: 14 additions & 0 deletions SfDataGridSample/SfDataGridSample/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version = "1.0" encoding = "UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:SfDataGridSample"
x:Class="SfDataGridSample.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
15 changes: 15 additions & 0 deletions SfDataGridSample/SfDataGridSample/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace SfDataGridSample
{
public partial class App : Application
{
public App()
{
InitializeComponent();
}

protected override Window CreateWindow(IActivationState? activationState)
{
return new Window(new AppShell());
}
}
}
15 changes: 15 additions & 0 deletions SfDataGridSample/SfDataGridSample/AppShell.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Shell
x:Class="SfDataGridSample.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:SfDataGridSample"
Shell.FlyoutBehavior="Flyout"
Title="SfDataGridSample">

<ShellContent
Title="Home"
ContentTemplate="{DataTemplate local:MainPage}"
Route="MainPage" />

</Shell>
10 changes: 10 additions & 0 deletions SfDataGridSample/SfDataGridSample/AppShell.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace SfDataGridSample
{
public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
}
}
}
58 changes: 58 additions & 0 deletions SfDataGridSample/SfDataGridSample/MainPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:syncfusion="clr-namespace:Syncfusion.Maui.DataGrid;assembly=Syncfusion.Maui.DataGrid"
xmlns:local="clr-namespace:SfDataGridSample"
x:Class="SfDataGridSample.MainPage">

<ContentPage.BindingContext>
<local:EmployeeViewModel x:Name="viewModel" />
</ContentPage.BindingContext>

<Grid ColumnDefinitions="*,300"
RowDefinitions="Auto,Auto">
<syncfusion:SfDataGrid x:Name="dataGrid"
Grid.Column="0"
Grid.Row="0"
AllowGroupExpandCollapse="True"
ColumnWidthMode="Auto"
GridLinesVisibility="Both"
HeaderGridLinesVisibility="Both"
AutoGenerateColumnsMode="None"
ItemsSource="{Binding Employees}">

<syncfusion:SfDataGrid.Columns>
<syncfusion:DataGridNumericColumn MappingName="EmployeeID"
HeaderText="Employee ID" />
<syncfusion:DataGridTextColumn MappingName="Title"
HeaderText="Designation" />
<syncfusion:DataGridDateColumn MappingName="HireDate"
HeaderText="Hire Date" />
<local:TextImageColumn MappingName="Title" />

</syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>

<syncfusion:SfDataGrid x:Name="dataGrid1"
Grid.Column="0"
Grid.Row="1"
ItemsSource="{Binding Employees}">
</syncfusion:SfDataGrid>

<VerticalStackLayout Grid.Column="1">
<Grid RowDefinitions="80,80">
<Button Text="Serialize"
Grid.Row="0"
Margin="0,0,0,20"
WidthRequest="150"
Clicked="Button_Clicked" />
<Button Text="Deserialize"
Grid.Row="1"
Margin="0,20,0,0"
WidthRequest="150"
Clicked="Button_Clicked_1" />
</Grid>
</VerticalStackLayout>
</Grid>

</ContentPage>
99 changes: 99 additions & 0 deletions SfDataGridSample/SfDataGridSample/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using Syncfusion.Maui.DataGrid;
using static SfDataGridSample.TextImageColumn;
using System.Globalization;
using System.Reflection;
using System.Runtime.Serialization;

namespace SfDataGridSample
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
dataGrid.CellRenderers.Add("TextImage", new TextImageColumnRenderer());
dataGrid1.CellRenderers.Add("TextImage", new TextImageColumnRenderer());
dataGrid.SerializationController = new SerializationControllerCustomExt(dataGrid);
dataGrid1.SerializationController = new SerializationControllerCustomExt(dataGrid1);
}

private void Button_Clicked(object sender, EventArgs e)
{
string localPath = Path.Combine(FileSystem.AppDataDirectory, "DataGrid.xml");
using (var file = File.Create(localPath))
{
dataGrid.Serialize(file);
}
}

private void Button_Clicked_1(object sender, EventArgs e)
{
string localPath = Path.Combine(FileSystem.AppDataDirectory, "DataGrid.xml");

using (var file = File.Open(localPath, FileMode.Open))
{
dataGrid1.Deserialize(file);
}
}
}

[DataContract(Name = "SerializableCustomGridColumn")]
public class SerializableCustomGridColumn : SerializableDataGridColumn
{
[DataMember]
public string? DateMappingName { get; set; }
}

public class SerializationControllerCustomExt : DataGridSerializationController
{
public SerializationControllerCustomExt(SfDataGrid sfGrid) : base(sfGrid) { }

protected override SerializableDataGridColumn GetSerializableColumn(DataGridColumn column)
{
if (column is TextImageColumn)
{
return new SerializableCustomGridColumn();
}
return base.GetSerializableColumn(column);
}

protected override void StoreColumnProperties(DataGridColumn column, SerializableDataGridColumn serializableColumn)
{
base.StoreColumnProperties(column, serializableColumn);

if (column is TextImageColumn textImageColumn && serializableColumn is SerializableCustomGridColumn customColumn)
{
customColumn.DateMappingName = textImageColumn.MappingName;
}
}

protected override DataGridColumn GetColumn(SerializableDataGridColumn serializableColumn)
{
if (serializableColumn is SerializableCustomGridColumn)
{
return new TextImageColumn();
}
return base.GetColumn(serializableColumn);
}

protected override void RestoreColumnProperties(SerializableDataGridColumn serializableColumn, DataGridColumn column)
{
base.RestoreColumnProperties(serializableColumn, column);

if (column is TextImageColumn textImageColumn && serializableColumn is SerializableCustomGridColumn customColumn)
{
if (!string.IsNullOrEmpty(customColumn.DateMappingName))
textImageColumn.MappingName = customColumn.DateMappingName;
}
}

public override Type[] KnownTypes()
{
var types = base.KnownTypes().ToList();
types.Add(typeof(SerializableCustomGridColumn));
return types.ToArray();
}

}

}
27 changes: 27 additions & 0 deletions SfDataGridSample/SfDataGridSample/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.Extensions.Logging;
using Syncfusion.Maui.Core.Hosting;

namespace SfDataGridSample
{
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureSyncfusionCore()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});

#if DEBUG
builder.Logging.AddDebug();
#endif

return builder.Build();
}
}
}
113 changes: 113 additions & 0 deletions SfDataGridSample/SfDataGridSample/Model/Employee.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using System.ComponentModel;

namespace SfDataGridSample
{
public class Employee : INotifyPropertyChanged
{
private int? _employeeID;
private string? _name;
private long _iDNumber;
private int _contactID;
private string? _loginID;
private int _managerID;
private string? _title;
private DateTime _birthDate;
private string? _maritalStatus;
private string? _gender;
private DateTime _hireDate;
private int _sickLeaveHours;
private double _salary;
private bool _employeeStatus;
private int _rating;
private bool _isButtonVisible;

public int? EmployeeID
{
get { return _employeeID; }
set { _employeeID = value; OnPropertyChanged(nameof(EmployeeID)); }
}
public string? Name
{
get { return _name; }
set { _name = value; OnPropertyChanged(nameof(Name)); }
}
public long IDNumber
{
get { return _iDNumber; }
set { _iDNumber = value; OnPropertyChanged(nameof(IDNumber)); }
}
public string? Title
{
get { return _title; }
set { _title = value; OnPropertyChanged(nameof(Title)); }
}
public int ContactID
{
get { return _contactID; }
set { _contactID = value; OnPropertyChanged(nameof(ContactID)); }
}
public DateTime BirthDate
{
get { return _birthDate; }
set { _birthDate = value; OnPropertyChanged(nameof(BirthDate)); }
}
public string? MaritalStatus
{
get { return _maritalStatus; }
set { _maritalStatus = value; OnPropertyChanged(nameof(MaritalStatus)); }
}
public string? Gender
{
get { return _gender; }
set { _gender = value; OnPropertyChanged(nameof(Gender)); }
}
public DateTime HireDate
{
get { return _hireDate; }
set { _hireDate = value; OnPropertyChanged(nameof(HireDate)); }
}
public int SickLeaveHours
{
get { return _sickLeaveHours; }
set { _sickLeaveHours = value; OnPropertyChanged(nameof(SickLeaveHours)); }
}
public double Salary
{
get { return _salary; }
set { _salary = value; OnPropertyChanged(nameof(Salary)); }
}
public string? LoginID
{
get { return _loginID; }
set { _loginID = value; OnPropertyChanged(nameof(LoginID)); }
}
public int ManagerID
{
get { return _managerID; }
set { _managerID = value; OnPropertyChanged(nameof(ManagerID)); }
}
public bool EmployeeStatus
{
get { return _employeeStatus; }
set { _employeeStatus = value; OnPropertyChanged(nameof(EmployeeStatus)); }
}
public int Rating
{
get { return _rating; }
set { _rating = value; OnPropertyChanged(nameof(Rating)); }
}

public bool IsButtonVisible
{
get { return _isButtonVisible; }
set { _isButtonVisible = value; OnPropertyChanged(nameof(IsButtonVisible)); }
}

public event PropertyChangedEventHandler? PropertyChanged;

protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Loading