DataGrid 'EditItem' is not allowed for this view when dragging multiple items
Asked Answered
I

6

7

I have a datagrid which gets data like this:

    public struct MyData
    {
        public string name { set; get; }
        public string artist { set; get; }
        public string location { set; get; }
    }

    DataGridTextColumn col1 = new DataGridTextColumn();
    col4.Binding = new Binding("name");
    dataGrid1.Columns.Add(col1);
    dataGrid1.Items.Add((new MyData() { name = "Song1", artist = "MyName", location =     "loc"}));
    dataGrid1.Items.Add((new MyData() { name = "Song2", artist = "MyName", location =     "loc2"}));

The problem is- whenever a user tries to edit a cell or drags multiple cells- the app throws an exception:

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

Why is this? Is it because of the way the data is entered?
Any ideas?
Thanks!

Insanity answered 4/8, 2011 at 21:53 Comment(0)
V
11

Instead of using a struct use a class instead.

UPDATED ANSWER: Try adding your MyData instances to a List then assigning that list to the DataGrid.ItemsSource

Vitrics answered 4/8, 2011 at 21:55 Comment(3)
I changed the 'struct' to 'class', but it still throws the exception.Insanity
@amitarios: Try adding your MyData instances to a List<MyData> then assigning that list to the DataGrid.ItemsSourceVitrics
Putting it into a list edited the list but not the source object. Am I missing something?Jory
C
12

I got this issue when assigning ItemsSource to IEnumerable<T>.

I fixed it by converting the IEnumberable<T> to a List<T> and then assigning that to ItemsSource.

I'm not sure why using IEnumerable caused that issue, but this change fixed it for me.

Chiaroscuro answered 4/12, 2012 at 9:19 Comment(2)
Because List<T> supports Add() and Remove()Cardiac
Agreed Jack Frost.Anjanette
V
11

Instead of using a struct use a class instead.

UPDATED ANSWER: Try adding your MyData instances to a List then assigning that list to the DataGrid.ItemsSource

Vitrics answered 4/8, 2011 at 21:55 Comment(3)
I changed the 'struct' to 'class', but it still throws the exception.Insanity
@amitarios: Try adding your MyData instances to a List<MyData> then assigning that list to the DataGrid.ItemsSourceVitrics
Putting it into a list edited the list but not the source object. Am I missing something?Jory
S
2

If you use datagrid DataGridCheckBoxColumn you need to set <Setter Property="IsEditing" Value="true" /> on check box column. See this: https://mcmap.net/q/149988/-how-to-perform-single-click-checkbox-selection-in-wpf-datagrid

Spinozism answered 3/9, 2012 at 8:40 Comment(0)
T
1

This answer is not my own, just the working code example suggested by AnthonyWJones.

public class MyData //Use class instead of struct
{
    public string name { set; get; }
    public string artist { set; get; }
    public string location { set; get; }
}

DataGridTextColumn col1 = new DataGridTextColumn();
col4.Binding = new Binding("name");
dataGrid1.Columns.Add(col1);
dataGrid1.Items.Add((new MyData() { name = "Song1", artist = "MyName", location =     "loc"}));
dataGrid1.Items.Add((new MyData() { name = "Song2", artist = "MyName", location =     "loc2"}));

//Create a list of MyData instances
List<MyData> myDataItems = new List<MyData>(); 
myDataItems.Add(new MyData() { name = "Song1", artist = "MyName", location =     "loc"});
myDataItems.Add(new MyData() { name = "Song2", artist = "MyName", location =     "loc2"});

//Assign the list to the datagrid's ItemsSource
dataGrid1.ItemsSource = items;
Tegument answered 13/3, 2014 at 17:56 Comment(0)
A
0

For my case,

processLimits.OrderBy(c => c.Parameter);

returns an

IOrderedEnumerable<ProcessLimits> 

not a

List<ProcessLimits>

so when I assign a style for my event setter to a checkbox column in my datagrid

style.Setters.Add(new EventSetter(System.Windows.Controls.Primitives.ToggleButton.CheckedEvent, new RoutedEventHandler(ServiceActiveChecked)));

ServiceActiveChecked is never called and I got

'EditItem' is not allowed for this view.

and for anyone else doing checkboxes in datagrid columns, I use a column object with my column data in this constructor for adding the data grid I use with adding the style above.

datagridName.Columns.Add(new DataGridCheckBoxColumn()
                            {
                                Header = column.HeaderText.Trim(),
                                Binding = new System.Windows.Data.Binding(column.BindingDataName.Trim()) { StringFormat = column.StringFormat != null ? column.StringFormat.Trim().ToString() : "" },
                                IsReadOnly = column.IsReadOnlyColumn,
                                Width = new DataGridLength(column.DataGridWidth, DataGridLengthUnitType.Star),
                                CellStyle = style,
                            });
Anjanette answered 15/2, 2018 at 14:44 Comment(0)
I
0

I solved this by setting the datagrid's source after the InitializeComponent:

    public MainWindow()
    {
        InitializeComponent();
        FilterGrid.ItemsSource = ScrapeFilter;
    }
Irade answered 10/1, 2020 at 9:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.