We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

SfDataGrid Deserialization Problem

Hi,

I have been using SfDataGrid in which I am using different type of bindings. There are converters, multi-binding and regular bindings with some GridComboBoxColumn,GridHyperlinkColumn and Template Columns. When i serialize my datagrid, everything seems ok. But when i deserialize it, some of my fields just wont bind. Instead there are filled with EntityFramework dynamic proxies. I attach an image of my SfDataGrid after deserialization. Below you can find xaml binding of the datagrid.

I appreciate any help
Thanks.




<syncfusion:SfDataGrid
                    Name="sfDataGrid"
                    Grid.Row="2"
                    AllowResizingColumns="True"
                    ShowGroupDropArea="True"
                    GroupDropAreaText="Alan gruplandirmalari için kolonlari sürükle ve birak"
                    AllowFiltering="True"
                    EditTrigger="OnDoubleTap"
                    ItemsSource="{Binding Datasheetcontents,UpdateSourceTrigger=PropertyChanged}"
                    AutoGenerateColumns="False"
                    SelectedItem="{Binding SelectedDataSheetContent,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
                    AllowDeleting="False"
                    AllowDraggingColumns="True"
                    AllowGrouping="True"
                    ShowColumnWhenGrouped="True"
                    AddNewRowPosition="None"
                    SelectionMode="Extended"
                    RowValidated="sfDataGrid_RowValidated"
                    SelectionChanged="sfDataGrid_SelectionChanged"
                    GridContextMenuOpening="sfDataGrid_GridContextMenuOpening">

                    <syncfusion:SfDataGrid.RecordContextMenu>
                        <ContextMenu>
                            <MenuItem Header="Sirayla Doldur" Click="MenuItem_Click" />
                            <MenuItem x:Name="meenuItemIcerik" Header="Içerik Sil" Click="menuItemIcerik_Click" />
                        </ContextMenu>
                    </syncfusion:SfDataGrid.RecordContextMenu>

                    <syncfusion:SfDataGrid.Columns>
                        <syncfusion:GridTextColumn HeaderText="AktiviteKodu" MappingName="budgetcode.activity.kod" AllowEditing="False"></syncfusion:GridTextColumn>
                        <syncfusion:GridTextColumn HeaderText="Bütçe Kodu" MappingName="budgetcodes_id" ValueBinding="{Binding budgetcodes_id,Converter={StaticResource BCConverter}}" AllowEditing="False"></syncfusion:GridTextColumn>
                        <syncfusion:GridTextColumn HeaderText="Bütçe Lokasyon Kodu" MappingName="budgetcode.locationcode.kod" AllowEditing="False"></syncfusion:GridTextColumn>
                        <syncfusion:GridTextColumn HeaderText="Imalat Grubu" MappingName="budgetcode.activity.imalatgrubu" AllowEditing="False"></syncfusion:GridTextColumn>
                        <syncfusion:GridTextColumn HeaderText="Ana Aktivite" MappingName="budgetcode.activity.anaaktivite" AllowEditing="False"></syncfusion:GridTextColumn>
                        <syncfusion:GridTextColumn HeaderText="Alt Aktivite" MappingName="budgetcode.activity.detay" AllowEditing="False"></syncfusion:GridTextColumn>
                        <syncfusion:GridTextColumn HeaderText="Birim" MappingName="budgetcode.activity.birim" AllowEditing="False"></syncfusion:GridTextColumn>
                        <syncfusion:GridTextColumn HeaderText="Ihale Kodu" MappingName="ihale_kod" AllowEditing="True"></syncfusion:GridTextColumn>
                        <syncfusion:GridTextColumn HeaderText="Bina Bölgesi/Etap" MappingName="etap" AllowEditing="True"></syncfusion:GridTextColumn>
                        <syncfusion:GridTextColumn HeaderText="Açiklama" MappingName="aciklama" AllowEditing="True"></syncfusion:GridTextColumn>
                        <syncfusion:GridTextColumn HeaderText="Mahal" MappingName="mahal" AllowEditing="True"></syncfusion:GridTextColumn>
                        <syncfusion:GridTextColumn HeaderText="Kat" MappingName="kat" AllowEditing="True"></syncfusion:GridTextColumn>
                        <syncfusion:GridTextColumn HeaderText="Kot" MappingName="kot" AllowEditing="True"></syncfusion:GridTextColumn>
                        <syncfusion:GridNumericColumn HeaderText="Yükseklik" MappingName="yukseklik" AllowEditing="True"></syncfusion:GridNumericColumn>
                        <syncfusion:GridNumericColumn HeaderText="Alan" MappingName="alan" AllowEditing="True"></syncfusion:GridNumericColumn>
                        <syncfusion:GridNumericColumn HeaderText="Çevre" MappingName="cevre" AllowEditing="True"></syncfusion:GridNumericColumn>
                        <syncfusion:GridNumericColumn HeaderText="Metraj" MappingName="metraj" AllowEditing="True"></syncfusion:GridNumericColumn>
                        <syncfusion:GridDateTimeColumn HeaderText="Baslangiç" ValueBinding="{Binding baslangic,UpdateSourceTrigger=PropertyChanged}" MappingName="baslangic" CustomPattern="dd.mm.yyyy" AllowEditing="True" AllowNullValue="True" NullText=""></syncfusion:GridDateTimeColumn>
                        <syncfusion:GridDateTimeColumn HeaderText="Bitis" ValueBinding="{Binding bitis,UpdateSourceTrigger=PropertyChanged}" MappingName="bitis" CustomPattern="dd.mm.yyyy" AllowEditing="True" AllowNullValue="True" NullText=" "></syncfusion:GridDateTimeColumn>
                        <syncfusion:GridComboBoxColumn HeaderText="Dagilim" ItemsSource="{Binding Distributions,UpdateSourceTrigger=PropertyChanged}" MappingName="distributions_id" SelectedValuePath="id" DisplayMemberPath="ad" AllowEditing="True"></syncfusion:GridComboBoxColumn>
                        <syncfusion:GridNumericColumn HeaderText="Bütçe Metraj" MappingName="metraj_budget" AllowEditing="True"></syncfusion:GridNumericColumn>
                        <syncfusion:GridHyperlinkColumn HeaderText="Fiyat" MappingName="." ValueBinding="{Binding .,Converter={StaticResource UnitPriceConverter}}" AllowEditing="False">
                            <syncfusion:GridHyperlinkColumn.CellStyle>
                                <Style>
                                    <Style.Resources>
                                        <Style TargetType="Hyperlink">
                                            <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}},Path=DataContext.fiyat_tooltip}"></Setter>
                                            <Setter Property="Foreground" Value="{Binding price_type,Converter={StaticResource UnitPriceColorConverter}}"></Setter>
                                            <Setter Property="Command" Value="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}},Path=DataContext.Show_Prices}"></Setter>
                                        </Style>
                                    </Style.Resources>
                                </Style>
                            </syncfusion:GridHyperlinkColumn.CellStyle>
                        </syncfusion:GridHyperlinkColumn>
                        <syncfusion:GridNumericColumn HeaderText="Birim Fiyat" MappingName="." AllowEditing="False" ValueBinding="{Binding .,Converter={StaticResource CurrenyConverterFiyat}}"></syncfusion:GridNumericColumn>
                        <syncfusion:GridTextColumn HeaderText="Birim Poz" AllowEditing="False" MappingName=".">
                            <syncfusion:GridTextColumn.ValueBinding>
                                <MultiBinding Converter="{StaticResource ConverterDSC}">
                                    <Binding Source="{StaticResource STR_poz}" />
                                    <Binding Path="." />
                                </MultiBinding>
                            </syncfusion:GridTextColumn.ValueBinding>
                        </syncfusion:GridTextColumn>
                        <syncfusion:GridTextColumn HeaderText="Kapsam" AllowEditing="False" MappingName=".">
                            <syncfusion:GridTextColumn.ValueBinding>
                                <MultiBinding Converter="{StaticResource ConverterDSC}">
                                    <Binding Source="{StaticResource STR_kapsam}" />
                                    <Binding Path="." />
                                </MultiBinding>
                            </syncfusion:GridTextColumn.ValueBinding>
                        </syncfusion:GridTextColumn>
                        <syncfusion:GridTextColumn HeaderText="Analiz Tipi" AllowEditing="False" MappingName=".">
                            <syncfusion:GridTextColumn.ValueBinding>
                                <MultiBinding Converter="{StaticResource ConverterDSC}">
                                    <Binding Source="{StaticResource STR_analiztip}" />
                                    <Binding Path="." />
                                </MultiBinding>
                            </syncfusion:GridTextColumn.ValueBinding>
                        </syncfusion:GridTextColumn>
                        <syncfusion:GridNumericColumn HeaderText="Tutar" MappingName="." AllowEditing="False" ValueBinding="{Binding .,Converter={StaticResource CurrenyConverterTutar}}"></syncfusion:GridNumericColumn>
                        <syncfusion:GridTemplateColumn HeaderText="Firma" MappingName="subcontractors_id" AllowEditing="True">

                            <syncfusion:GridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding subcontractor.ad}"></TextBlock>
                                </DataTemplate>
                            </syncfusion:GridTemplateColumn.CellTemplate>
                            <syncfusion:GridTemplateColumn.EditTemplate>
                                <DataTemplate>
                                    <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.FilteredSubcontractors}" DisplayMemberPath="ad" SelectedValuePath="id" SelectedValue="{Binding subcontractors_id}"></ComboBox>
                                </DataTemplate>
                            </syncfusion:GridTemplateColumn.EditTemplate>
                        </syncfusion:GridTemplateColumn>
                        <syncfusion:GridTemplateColumn HeaderText="Sözlesme No" MappingName="contracts_id" AllowEditing="True">
                            <syncfusion:GridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding contract.sozlesme_no}"></TextBlock>
                                </DataTemplate>
                            </syncfusion:GridTemplateColumn.CellTemplate>
                            <syncfusion:GridTemplateColumn.EditTemplate>
                                <DataTemplate>
                                    <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.FilteredContracts}" DisplayMemberPath="sozlesme_no" SelectedValuePath="id" SelectedValue="{Binding contracts_id}"></ComboBox>
                                </DataTemplate>
                            </syncfusion:GridTemplateColumn.EditTemplate>
                        </syncfusion:GridTemplateColumn>
                        <syncfusion:GridTemplateColumn HeaderText="Ihale Paketi" MappingName="tenderpackages_id" AllowEditing="True">
                            <syncfusion:GridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding tenderpackage.ad}"></TextBlock>
                                </DataTemplate>
                            </syncfusion:GridTemplateColumn.CellTemplate>
                            <syncfusion:GridTemplateColumn.EditTemplate>
                                <DataTemplate>
                                    <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.TenderPackages}" DisplayMemberPath="ad" SelectedValuePath="id" SelectedValue="{Binding tenderpackages_id}"></ComboBox>
                                </DataTemplate>
                            </syncfusion:GridTemplateColumn.EditTemplate>
                        </syncfusion:GridTemplateColumn>
                        <syncfusion:GridTextColumn HeaderText="Fiyat Tip" MappingName="price_type" AllowEditing="False"></syncfusion:GridTextColumn>
                        <syncfusion:GridTextColumn HeaderText="Fiyat ID" MappingName="." AllowEditing="False">
                            <syncfusion:GridTextColumn.ValueBinding>
                                <MultiBinding Converter="{StaticResource ConverterDSC}">
                                    <Binding Source="{StaticResource STR_fiyatid}" />
                                    <Binding Path="." />
                                </MultiBinding>
                            </syncfusion:GridTextColumn.ValueBinding>
                        </syncfusion:GridTextColumn>
                        <syncfusion:GridTextColumn HeaderText="ID" MappingName="id"></syncfusion:GridTextColumn>
                        <syncfusion:GridHyperlinkColumn HeaderText="Içerik" MappingName="contentfile" ValueBinding="{Binding contentfile,Converter={StaticResource IcerikConverter}}" AllowEditing="False">
                            <syncfusion:GridHyperlinkColumn.CellStyle>
                                <Style>
                                    <Style.Resources>
                                        <Style TargetType="Hyperlink">
                                            <Setter Property="Foreground" Value="{Binding contentfile,Converter={StaticResource IcerikColorConverter}}"></Setter>
                                            <Setter Property="Command" Value="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}},Path=DataContext.Manage_Icerik}"></Setter>
                                        </Style>
                                    </Style.Resources>
                                </Style>
                            </syncfusion:GridHyperlinkColumn.CellStyle>
                        </syncfusion:GridHyperlinkColumn>
                    </syncfusion:SfDataGrid.Columns>
                </syncfusion:SfDataGrid>






1 Reply

JG Jai Ganesh S Syncfusion Team March 14, 2016 02:14 PM UTC

Hi Kemal


In your code snippet, you have used binding and multi binding in columns . We regret to inform you that the SfDataGrid does not have a support to serialize the binding datas. If you want to serialize the binding datas then you can write your own code to save the binding data as a XML format using XamlWriter.


MSDN Forum Link:https://social.msdn.microsoft.com/Forums/vstudio/en-US/8fbde488-c310-453b-b80c-5571e7631ce6/how-to-serialize-binding-and-data-templates-in-c?forum=wpf


Also you have set the MappingName as dot for some columns, and it will be treated as complex properties.
http://help.syncfusion.com/wpf/sfdatagrid/data-binding#binding-complex-properties


And also you have used same MappingName (.)for some columns. In SfDataGrid all operations (Sorting, Filtering and Grouping etc. based on MappingName only). So could you please use the separate MappingName for columns.


Also, you have also used GridTemplateColumn in your application. In SfDataGrid it is not possible to Serialize GridTemplateColumn. However you can achieve your requirement by manually adding the CellTemplate to the GridTemplateColumn by overriding the SerializationController like the below code example. We have prepared a work around sample as per your requirement and you can download the same from the below location,

C#


 this.dataGrid.SerializationController = new SerializationControllerExt(this.dataGrid);


    public class SerializationControllerExt : SerializationController

    {

        public SerializationControllerExt(SfDataGrid grid)

            : base(grid)

        {


        }


        //Serialization

        protected override SerializableGridColumn GetSerializableGridColumn(GridColumn column)

        {

            return base.GetSerializableGridColumn(column);

        }


        protected override void StoreGridColumnProperties(GridColumn column, SerializableGridColumn serializableColumn)

        {

            base.StoreGridColumnProperties(column, serializableColumn);

        }


        //Deserialization

        protected override GridColumn GetGridColumn(SerializableGridColumn serializableColumn)

        {

            return base.GetGridColumn(serializableColumn);

        }


        protected override void RestoreColumnProperties(SerializableGridColumn serializableColumn, GridColumn column)

        {

            base.RestoreColumnProperties(serializableColumn, column);

            if (column is GridTemplateColumn)

            {

                if (column.MappingName == "SupplierID")

                {

                    column.CellTemplate = App.Current.Resources["TemplateColumn"] as DataTemplate;

                }

            }

        }

    }


Sample: http://www.syncfusion.com/downloads/support/directtrac/153551/ze/SerializationDemo940815637


UG Link: http://help.syncfusion.com/wpf/sfdatagrid/serialization-and-deserialization#overriding-serializationcontroller


Regards,

Jai Ganesh S



Loader.
Up arrow icon