2

While operating on a DataGrid I am receiving this error:

System.InvalidOperationException: ''DeferRefresh' is not allowed during an AddNew or EditItem transaction.'

How does one resolve this?

Code

I have two lists of objects "Feeders" and "Eaters". Each object has a property called FedFrom, which points to a Feeder object by Tag. The way my data is setup is actually using "Tags" I store the Tag in the "FedFrom" property as a string instead of the entire "Feeder" object. Also, FedFrom can be blank

Feeders

Feeder Tag (textbox) FedFrom (FeederTag) (combobox)
fTagA 'blank'
fTagB fTagA
fTagC fTagB
fTagD fTagC

Eaters

Eater Tag (textbox) FedFrom (FeederTag) (combobox)
eTagA fTagD
eTagB fTagC
eTagC fTagC
eTagD fTagB

I display two DataGrids one for Feeders and one for eaters. The FedFrom column on both grids is a combobox column that lists all the available Feeders in the ObservableCollection. When I add new feeders everything updates correctly. In the eaters datagrid, the FedFrom column combobox sees the newly added Feeders and I assume it also works in the feeders grid as well, but I get the following error as soon as I click on the FedFrom combobox in the Feeders datagrid:

System.InvalidOperationException: ''DeferRefresh' is not allowed during an AddNew or EditItem transaction.'

I think this makes sense as I'm self referencing the collection, so my question is how do I get around this? I'm just using basic WPF binding. The ItemsSource for each combobox is the Feeders collection with DisplayMemberPath = "Tag".

This done in the code behind so that I can control how columns are autogenerated. Not sure if this is an issue.

ObservableCollection<Feeder> Feeders = new ObservableCollection<Feeder>();
ObservableCollection<Eater> Eaters = new ObservableCollection<Eater>();

public class Feeder
{
   Tag {get; set;}
   FedFrom {get; set;}  //this is where I want to store feeder tag
}

public class Eater
{
   Tag {get; set;}
   FedFrom {get; set;}  //this is where I want to store feeder tag
}

private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) 
{
    if (e.PropertyName == "FedFrom") 
    {
        var cb = new DataGridComboBoxColumn();
        cb.ItemsSource = DteqList;
        cb.SelectedValuePath = "Tag";
        cb.DisplayMemberPath = "Tag";
        cb.SelectedValueBinding = new Binding("FedFrom"); //allows binding to the property

        cb.CellStyle  = new Style(typeof(DataGridCell));
        cb.CellStyle.Setters.Add(new Setter(BackgroundProperty, Brushes.Transparent));
        cb.CellStyle.Setters.Add(new Setter(ForegroundProperty, Brushes.Black));
        cb.Header = ((PropertyDescriptor)e.PropertyDescriptor).DisplayName;
        e.Column = cb;
    }
}
dbc
  • 104,963
  • 20
  • 228
  • 340
pdeault
  • 21
  • 1
  • 5
  • The answer posted here was the solution: https://stackoverflow.com/questions/3461714/wpf-datagrid-combobox-causes-invalidoperationexception – pdeault Dec 06 '21 at 16:27
  • Find the exact answer here https://stackoverflow.com/questions/3461714/wpf-datagrid-combobox-causes-invalidoperationexception – pdeault Dec 06 '21 at 19:17
  • I'll go ahead and make this a duplicate of https://stackoverflow.com/questions/3461714/wpf-datagrid-combobox-causes-invalidoperationexception then. Thanks for letting us know the resolution! – dbc Dec 11 '21 at 00:38

0 Answers0