Is it possible to edit a DataGrid that uses a CompositeCollection as its ItemsSource?

Oct 16, 2008 at 2:58 PM
When I set the ItemsSource for a DataGrid to a CompositeCollection, I lose my ability to edit rows in the grid. Clicking on a cell in the grid gives the following error:

'EditItem' is not allowed for this view

It seems reasonable that I would not be able to add any rows via the grid, since the grid would not know which collection to add the items to, however I would still expect to be able to edit each existing row. Is this a bug in the DataGrid?

Here is some sample code to illustrate this problem:

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Data;

namespace WpfDatagridSandbox {
    public class Person {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public Person(string firstName, string lastName) {
            FirstName = firstName;
            LastName = lastName;
        }
    }

    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window {
        public Window1() {
            InitializeComponent();

            IList<Person> persons = new ObservableCollection<Person>();
            for (int i = 0; i<10; i++) {
                persons.Add(new Person("Jim " + i, "Smith " + i));
            }
            DataContext = new CompositeCollection {new CollectionContainer {Collection = persons}};
            //            DataContext = persons;
        }
    }
}

The XAML for the window looks like this:

<Window x:Class="WpfDatagridSandbox.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Controls="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
    Title="Window1" Height="300" Width="300">
    <Controls:DataGrid Name="datagrid" AutoGenerateColumns="True" ItemsSource="{Binding}">
        <Controls:DataGrid.Columns>
            <Controls:DataGridTextColumn DataFieldBinding="{Binding Path=FirstName}" Header="First" MinWidth="50" />
            <Controls:DataGridTextColumn DataFieldBinding="{Binding Path=LastName}" Header="Last" MinWidth="50" />
        </Controls:DataGrid.Columns>
    </Controls:DataGrid>
</Window>

Thanks in advance!



Coordinator
Oct 20, 2008 at 5:20 PM
Only ListCollectionView and BindingListCollectionView implement IEditableCollectionView.  Since CompositeCollectionView does not you will not get editing, adding, or deleting capabilities.