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
close icon

Problem with combobox column

good morning,

I have a strange behavior in the comboboxes of the grid, I have a framework entity connected with postgre, I am going to focus on only one of the comboboxes, but I have the main object:

public class Transactions

    {

        public virtual long id { set; get; }


        public virtual DateTime date { set; get; }


        public virtual Accounts? account { set; get; }


        public virtual Persons? person { set; get; }


        public virtual Categories? category { set; get; }


        public virtual Decimal? amount { set; get; }


        public virtual Double? orden { set; get; }


        [NotMapped]

        public virtual Decimal? balance { set; get; }


    }

Then I have the person object that belongs to the above:

public class Persons

    {

        [Key]

        public virtual long id { set; get; }


        public virtual String? name { set; get; }

    }


The grid would be this:

<GridSplitter Grid.Column="1" Width="5" HorizontalAlignment="Stretch" DragDelta="GridSplitter_DragDelta" />

        <syncfusion:SfDataGrid Grid.Column="2" x:Name="gvMovimientos"

                               AutoGenerateColumns="False" AllowFiltering="true" AllowEditing="True" AllowSorting="False"

                               AllowResizingColumns="True" AddNewRowPosition="FixedTop" AllowDeleting="True"

                               RowValidated="gvMovimientos_RowValidated" CurrentCellDropDownSelectionChanged="gvMovimientos_CurrentCellDropDownSelectionChanged"

                               EditTrigger="OnTap"  ScrollMode="Async">

            <syncfusion:SfDataGrid.SortColumnDescriptions>

                <syncfusion:SortColumnDescription ColumnName="orden" SortDirection="Descending"/>

            </syncfusion:SfDataGrid.SortColumnDescriptions>

            <syncfusion:SfDataGrid.Columns>

                <syncfusion:GridTextColumn MappingName="id" IsHidden="True" />

                <syncfusion:GridDateTimeColumn MappingName="date" HeaderText="Fecha"/>

                <syncfusion:GridComboBoxColumn  HeaderText="Cuenta"

                    AllowFiltering="True" IsEditable="True" IsCaseSensitiveFilterRow="False"

                    x:Name="cbAccounts" MappingName="account.id"

                                                DisplayMemberPath="description" SelectedValuePath="id"/>

                <syncfusion:GridComboBoxColumn  HeaderText="Persona"

                    AllowFiltering="True" IsEditable="True" IsCaseSensitiveFilterRow="False"

                    x:Name="cbPersons" MappingName="person.id"

                                                DisplayMemberPath="name" SelectedValuePath="id" />

                <syncfusion:GridComboBoxColumn  HeaderText="Categoría"

                    AllowFiltering="True" IsEditable="True" IsCaseSensitiveFilterRow="False"

                    x:Name="cbCategories" MappingName="category.id"

                                                DisplayMemberPath="description" SelectedValuePath="id" />

                <syncfusion:GridCurrencyColumn MappingName="amount" HeaderText="Cantidad"/>

                <syncfusion:GridCurrencyColumn MappingName="balance" HeaderText="Saldo"/>

                <syncfusion:GridTextColumn MappingName="orden" IsHidden="True" />

            </syncfusion:SfDataGrid.Columns>


        </syncfusion:SfDataGrid>


And in the code of the page I have this:

private ObservableCollection<Transactions>? viewTransaction;

.

.

.

private void loadContext()

        {

            AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);

            rycContext = new RYCContext();

            rycContext.categories?.Load();

            rycContext.persons?.Load();

            rycContext.accountsTypes?.Load();

            rycContext.accounts?.Load();

            rycContext.transactions?.Load();           

        }


private void frmInicio_Loaded(object sender, RoutedEventArgs e)

        {

if (rycContext?.transactions != null)

            {

                viewTransaction = new ObservableCollection<Transactions>(from x in rycContext.transactions

                                                                         select x);

                gvMovimientos.ItemsSource = CollectionViewSource.GetDefaultView(rycContext.transactions.ToList());

            }


            gvMovimientos.ItemsSource = viewTransaction;

cbAccounts.ItemsSource = rycContext?.accounts?.ToList();


            if (rycContext?.persons != null)

            {

                cbPersons.ItemsSource = new ObservableCollection<Persons>(rycContext.persons);

            }


            cbCategories.ItemsSource = rycContext?.categories?.ToList();


}

private void gvMovimientos_RowValidated(object sender, Syncfusion.UI.Xaml.Grid.RowValidatedEventArgs e)

        {

            Transactions tr = (Transactions)e.RowData;

            Transactions t = rycContext.transactions.Single(x => x.id == tr.id);           

            t.orden = Double.Parse(t.date.Year.ToString("0000")

                    + t.date.Month.ToString("00")

                    + t.date.Day.ToString("00")

                    + t.id.ToString("000000")

                    + (t.amount < 0 ? "1" : "0"));

            rycContext.Entry(t).State = EntityState.Modified;

            rycContext?.SaveChanges();

            Task.Factory.StartNew(refreshBalance);  

        }

I have tried to have = rycContext?.person?.ToList() in the data load but it is the same, what it does is that when I change the value of the combo it changes the value of the text of persons.name for the id that it has in database that row, the entire object does not change, so when saving I get the error:


The property 'Persons.id' is part of a key and so cannot be modified or marked as modified....


It must be that I have something wrong configured, I also attach some images if necessary.



1 Reply 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team October 11, 2022 01:43 PM

Hi Ricardo,

We would like to let you know that the cell value of GridComboBoxColumn will be updated only while changing the underlying property (MappingName) that is bound to the column. The reported problem occurs because the MappingName bound property is a class (ex: person) type and not a property. Please refer to the below screenshot.



However, you can resolve the reported problem by defining the datatype (ex: long) not the class type that MappingName bound with GridComboBoxColumn. Please refer to the below screenshot,


XAML Code Snippet of GridComboBoxColumn:

  <syncfusion:GridComboBoxColumn  HeaderText="Persona"

 

                                  AllowFiltering="True" IsEditable="True"

                                 

                                  IsCaseSensitiveFilterRow="False"

 

                                  x:Name="cbPersons" MappingName="person"

 

                                  DisplayMemberPath="name" SelectedValuePath="id" />


Please find the sample in the attachment. For more information related to GridComboBoxColumn, please refer to the below user guide documentation link,

UG Link: https://help.syncfusion.com/wpf/datagrid/column-types#gridcomboboxcolumn

KB Link:
https://www.syncfusion.com/kb/7737/how-to-bind-the-view-model-itemssource-to-gridcomboboxcolumn-in-wpf-datagrid-sfdatagrid

https://www.syncfusion.com/kb/6701/how-to-load-the-collection-in-gridcomboboxcolumn-when-defining-datacontext-after-loading


If we misunderstood your requirement, please provide more information regarding the requirement. This would help us to proceed further.

Regards,

Vijayarasan S


If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.



Attachment: SfDataGridDemo_f81b05d0.zip

Marked as answer
Loader.
Live Chat Icon For mobile
Up arrow icon