Why is one of MY WPF DataGrids giving an “EditItem” exception for this view exception?

I read all the Q&A that I could find here and on the MS forums about this exception, and tried most of the suggestions I understood, and several others. It seems that this exception can occur for a number of reasons.

As with others, I have a WPF DataGrid binding to a collection that throws this exception when you try to edit one of the cells. They are configured for write capabilities, the collection is ObservableCollection, I implemented get and set handlers that send notifications.

The suggestions that I have not tried are those related to the implementation of the non-original IList interface, because I have no idea what I will do for this. In addition, I have many DataGrids linked to various lists and collections in my application that work, and this one was used when it was associated with the LINQ collection.

Please help me figure out what I need to do here.

Data Grid:

<DataGrid Name="dgIngredients" Margin="567,32,0,44" Width="360" ItemsSource="{Binding}" IsReadOnly="False"
           AutoGenerateColumns="False" HorizontalAlignment="Left" CanUserAddRows="False" CanUserDeleteRows="False">
    <DataGrid.Columns>
        <DataGridTextColumn Width="63" Header="Percent" Binding="{Binding Preference}" IsReadOnly="False" />
        <DataGridTextColumn SortDirection="Descending" Width="301" Header="Ingredient" Binding="{Binding Ingredient}" IsReadOnly="True" CanUserSort="True" CanUserReorder="False" />
    </DataGrid.Columns>
</DataGrid>

Editable column is optional. Preference.

Collection:

private ObservableCollection<RAM_Ingredient> MemberIngredientPrefs = new ObservableCollection<RAM_Ingredient>();

Binding:

dgIngredients.DataContext = MemberIngredientPrefs.OrderBy("Ingredient",true);

RAM_Ingredient:

public class RAM_Ingredient : INotifyPropertyChanged 

and etc.

Where RAM_Ingredient.Preference:

private int _Preference;
public int Preference
{
    get
    {
        return _Preference;
    }
    set
    {
        // This is needed to send notification of changes (and to not throw an exception on grid edit!):
        if ((_Preference != value))
        {
            SendPropertyChanging();
            _Preference = value;
            SendPropertyChanged("Preference");
        }
    }
}

The exception is:

System.InvalidOperationException was unhandled
  Message='EditItem' is not allowed for this view.
  Source=PresentationFramework
  StackTrace:
       at System.Windows.Controls.ItemCollection.System.ComponentModel.IEditableCollectionView.EditItem(Object item)
       at System.Windows.Controls.DataGrid.EditRowItem(Object rowItem)
       at System.Windows.Controls.DataGrid.OnExecutedBeginEdit(ExecutedRoutedEventArgs e)

etc...

+2
source share
5 answers

I also have this problem. And it turned out that the point here is that we cannot edit IEnumerable in the DataGrid, only the list can be edited.

, LINQ . .

:

 dtgPrdcts.ItemsSource= ProductLists.Select(Function(x) New With {.ListTitle = x.ListTitle, .ProductID = x.ProductID, .License = "", .ForRemove = True}).ToList
+4

, , , , , , , .

DataGrid. DataGrid, . , " " (, ), -, - .

.

List<UsablePref> MemberIngredientPrefs = new List<UsablePref>();

...

            foreach (RAM_Ingredient ingredient in App.Ingredients)
        {
            ingredient.GetPreferences(EditorMember);
            UsablePref pref = new UsablePref();
            pref.Ingredient = ingredient.Ingredient;
            pref.IngredientID = ingredient.IngredientID;
            pref.Preference = ingredient.Preference;
            MemberIngredientPrefs.Add(pref);
        }

        // Sort alphabetically by ingredient name, 
        MemberIngredientPrefs.Sort(UsablePref.CompareByName);
        // and bind the ingredient prefs DataGrid to its corresponding List
        dgIngredients.DataContext = MemberIngredientPrefs;
+2

, ; LINQ IEnumerable, DataGrid IEnumerable; readonly ComboBoxes , , InvalidOperationException. ObservableCollection IEnumerable; :

BoundView = (/*LINQ QUERY*/); // is IEnumerable<CustomJoinObject>

BoundView = new ObservableCollection<CustomJoinObject>(/*LINQ QUERY*/);

BoundView DataContext DataGrid.

, , IEnumerable datagrid, ObservableCollection .

+1

INotifyPropertyChanged System.ComponentModel.

:

 public class Exemple :  INotifyPropertyChanged
 {
   #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        #endregion INotifyPropertyChanged Members

 }
0

3

case1: , ( , , )

Datagrid IsReadOnly = "True"

case2: , , RowEditEnding

  (sender as DataGrid).CommitEdit(DataGridEditingUnit.Row);

case3: RowEditEnding, , ; , ,

, -

0

All Articles