NeverAgain
NeverAgain

Reputation: 76

WPF DataGrid Editing Difficulty (InvalidOperationException)

I have a DataGrid that I am filling with information from an ObservableCollection<ObservableCollection<T>> where T is an object of mine that implements IEditableObject.

I have the DataGrid correctly filling with the information I need. The problem is that when I go to edit a box in it I get an exception.

System.InvalidOperationException: 'EditItem' is not allowed for this view

followed by the subsequent stack trace.

I understand binding as well as the idea that in an ObservableCollection<T> if you want to be able to edit the DataGrid items than you need to implement the IEditableObject interface in the object T.

I'm not sure why it won't let me edit, although I'm leaning more towards the idea that I need to attach my object to some type of view.

I've not found much on what I'm trying to do but I have been fairly successful up until this point in getting over hurdles. If anyone has any insight as to what I should do it is much appreciated.

Also, I will post code if someone would seriously like to see it but I believe it's less about what I've already written and more about what I still need to write.

Edit: Added code

private void dg_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        DataGrid grid = sender as DataGrid;
        grid.Columns.Clear();
        TableNode node = e.NewValue as TableNode;
        if (node != null)
        {
            InfoTable dti = new InfoTable(m_model.EditNode.Database.Path, node.FullName);
            int index = 0;
            foreach (string colName in dti.Columns)
            {
                DataGridTextColumn col = new DataGridTextColumn();
                col.Header = colName;
                col.Binding = new Binding("[" + index + "].Info");
                //Note: .Info is the information string in the class T containing the data that may or may not be edited
                grid.Columns.Add(col);
                index++;
            }
            grid.ItemsSource = dti;
        }
    }

Here I am binding the columns to each ObservableCollection in the ObservableCollection<ObservableCollections<T>> that is in my InfoTable. I have implemented IEnumerable and INotfiyPropertyChange in the InfoTable.

Upvotes: 1

Views: 599

Answers (1)

almulo
almulo

Reputation: 4978

I'm pretty sure your InfoTable class needs to implement IList, too, in order to support DataGrid editing.

IList adds the capability of adding and removing items, and the DataGrid probably needs that to manage its rows.

Upvotes: 2

Related Questions