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

匆匆过客 提交于 2019-12-19 02:28:56

问题


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!


回答1:


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




回答2:


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.




回答3:


If you use datagrid DataGridCheckBoxColumn you need to set <Setter Property="IsEditing" Value="true" /> on check box column. See this: https://stackoverflow.com/a/12244451/1643201




回答4:


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;



回答5:


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,
                            });


来源:https://stackoverflow.com/questions/6949022/datagrid-edititem-is-not-allowed-for-this-view-when-dragging-multiple-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!