DataGrid edition 'EditItem' is not allowed for this view` when bound to a WPF DataGrid

datagrid js
c# datagrid vs datagridview
wpf datagrid binding
datagrid xaml
wpf datagrid itemssource
wpf datagrid columns
datagrid devextreme
wpf editable datagrid example

I've been reading about this at least for 4 hours, and seems to be the list type, but I have a situation:

A ObservableCollection that has a collection property.

I define the first DataGrid, and in the section

<DataGrid.RowDetailsTemplate>
    <DataTemplate>
        <!-- second Datagrid here, binding to Level2 property of my Observable collection -->
    </DataTemplate>
<DataGrid.RowDetailsTemplate>

Everything goes fine, all things on screen as I expected... but:

  1. If try to modify DataGrid1 cells it allow me.
  2. If try to modify DataGrid2 cells it throw me this exception 'EditItem' is not allowed for this view

What am I missing ?

This is my model:

public partial class Level1
{
    public Level1()
    {
        this.Level2 = new HashSet<Level2>();
    }

    public decimal IdLevel1 { get; set; }
    public decimal IdLevel2 { get; set; }
    public string StrDescripcionTipoAsociado {get;set;}

    public virtual Level2 Level2{ get; set; }
}

public partial class Level2
{
    public decimal IdLevel1 { get; set; }
    public decimal IdLevel3 { get; set; }

    public virtual Level3 Level3{ get; set; }
}

public partial class Level3
{
    public decimal IdLevel3 { get; set; }
    public decimal NumIdConcepto {get;set;}
    public string StrDescripcionConcepto {get;set;}
}

EDIT: XAML Code:

    <DataGrid Grid.Row="1" 
              ItemsSource="{Binding Level1}" 
              AutoGenerateColumns="False" 
              SelectionMode="Single"
              GridLinesVisibility="Vertical"
              CanUserAddRows="True"
              CanUserDeleteRows="True"
              x:Name="GridTipoAsociado">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Tipo de asociado" x:Name="TipoUsuarioSeleccionado">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Label Style="{StaticResource ResourceKey=FontElemNivel1}" Content="{Binding StrDescripcionTipoAsociado}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <TextBox Style="{StaticResource ResourceKey=FontElemNivel2}" Text="{Binding StrDescripcionTipoAsociado }"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
        <DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <DataGrid Grid.Row="1" 
                          ItemsSource="{Binding Level2}" 
                          AutoGenerateColumns="False" 
                          SelectionMode="Single"
                          SelectionUnit="Cell"
                          GridLinesVisibility="Vertical"
                          CanUserAddRows="True"
                          CanUserDeleteRows="True"                            
                          x:Name="GridItems">
                    <DataGrid.Columns>
                        <DataGridTemplateColumn Header="Id Item">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <Label Content="{Binding NumIdConcepto}"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                        <DataGridTemplateColumn Header="Items">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <Label Content="{Binding Level3.StrDescripcionConcepto}"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                            <DataGridTemplateColumn.CellEditingTemplate>
                                <DataTemplate>
                                    <TextBox Text="{Binding Level3.StrDescripcionConcepto}"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellEditingTemplate>
                        </DataGridTemplateColumn>
                    </DataGrid.Columns>
                </DataGrid>
            </DataTemplate>
        </DataGrid.RowDetailsTemplate>
    </DataGrid>

I tried this and the problem is that you have initialized your Level2 collection as Hashset<>. IEditableCollectionView.EditItem() throws this error while trying to update item in Hashset<>. I initialized the collection as List<> and it was working fine.

I am not sure why it is not able to update item in hashset, need to look deeper into it. But changing the Hashset<> to List<> will fix this error.

Hope it helps

Thanks

DataGrid, Discussion of topic Datagrid - How to set EditingElementStyle DataGridTextColumn in WPF Edition forum. Use Data Grid to create enterprise-grade data grid interfaces. Enable your users to access, explore, and edit large datasets, in a responsive interface. Giving your users an Excel-like UI is more suitable and time-effective than designing a custom solution every time they need to manipulate dense datasets in bulk efficiently.

You might try this. Attach a BeginningEdit handler to your DataGrid and point to this code:

    private void Grid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
    {
        //// Have to do this in the unusual case where the border of the cell gets selected
        //// and causes a crash 'EditItem is not allowed'
        e.Cancel = true;
    }

This will only hit if you somehow manage to physically tap down on the border of the cell. The event's OriginalSource is a Border, and I think what may happen here is instead of a TextBox, or other editable element being the source as expected, this un-editable Border comes through for editing, which causes an exception that is buried in the 'EditItem is not allowed' exception. Canceling this RoutedEvent before it can bubble on through with its invalid OriginalSource stops that error occurring in its tracks.

Datagrid | WPF Edition, What is difference between DataGridView and DataGrid control in Winforms? Refactorings that work in SQL files and schemas. DataGrip correctly resolves all references in your SQL code and helps you refactor them. When you rename a variable or an alias, it will update their usages throughout the entire file.

Tks to @nit who give me the right path. Of course the problem reside on the base Type of EF collections

Hashet< T > And Datagrid need at least a List< T >, changing all my classes "those generated by Entity framework", give to me another problem, must make changes manually, and I have a lot of them.

My solution was to create a converter, that made the dirty work for me:

public class listToObservableCollection : BaseConverter, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        HashSet<Level2> observableList = (HashSet<Level2>)value;
        return new ObservableCollection<Level2>(observableList);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return (HashSet<Level2>)value;
    }
}

public abstract class BaseConverter : MarkupExtension
{
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}

And put it on the binding of my Datagrid2:

<!--part of my window definition--!>
xmlns:l="clr-namespace:Recursos;assembly=Recursos"
...
<!--part of my resources section--!>
<l:listToObservableCollection x:Key="listoToObservable"/>
...
<!--part of my datagrid definition--!>
ItemsSource="{Binding Level2,Converter={StaticResource listoToObservable}}"

The only thing on the air is how to make a generic converter, but for now it works fine.

WPF - Datagrid, control is highly configurable and extensible, and it provides many properties, methods, and events to customize its appearance and behavior. ag-Grid is a feature-rich datagrid available in Free or Enterprise versions. This page covers downloading the JavaScript packages for ag-Grid and ag-Grid Enterprise.

I also solved this problem by using IsReadOnly="True".

Differences Between DataGridView and DataGrid Controls , The blazing-fast DevExtreme HTML5 JavaScript Data Grid is a feature-rich data shaping and editing client-side widget allowing your end-users to easily manage​  Examples. The following code example creates a Windows form, a DataSet containing two DataTable objects, and a DataRelation that relates the two tables. To display the data, a System.Windows.Forms.DataGrid control is then bound to the DataSet through the SetDataBinding method.

Here is a generic Converter that I used

public class ObservableCollectionConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var observableType= typeof (ObservableCollection<>).MakeGenericType(value.GetType().GetGenericArguments());
        return Activator.CreateInstance(observableType, value);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var observableType = typeof(HashSet<>).MakeGenericType(value.GetType().GetGenericArguments());
        return Activator.CreateInstance(observableType, value);
    }
}

C# DataGridView Tutorial, You can prevent a column from displaying in the first place by setting the columnnames attribute of the DataGrid to an array containing the names of the columns  To show a basic data grid , just drop a DataGrid control to your view and bind the ItemsSource to a collection of data objects and you're done. The DataGrid provides a feature called AutoGenerateColumns that automatically generates column according to the public properties of your data objects. It generates the following types of columns:

WPF - Controls, Datagrid The Datagrid has four tags namely datagrid , datagridColumn , datagridCheckbox and datagridSelect . The datagrid is used to allow modification and  DataGrid edition 'EditItem' is not allowed for this view` when bound to a WPF DataGrid. Ask Question Asked 6 years, 9 months ago. Active 7 days ago.

DevExtreme DataGrid, DataGrid\themes, Specifies the templates for different styles and the initial style of the DataGrid. Common.xaml, C1.WPF.DataGrid\themes, Specifies attributes for  March 5, 2018, released v3.3.0 with a major update to the DataGrid and 35 bug fixes and improvements. Jan. 15, 2018, released v3.6.0 of the Plus Edition released with 1 new control and 40 bug fixes and improvements .

Special Edition Using Macromedia Studio MX 2004, The robust and data-bound WPF DataGrid makes it easy to display, edit, analyze, and export tabular data in your WPF applications. NET Web Forms Edition  WPF DataGrid. WPF DataGrid control displays tabular data in a grid format. In this tutorial, you&#39;ll learn how to use a WPF DataGrid control, set its properties, and display data from a collection.

Comments
  • Can you post the xaml for DataGrid2 and code behind for its itemsSource collection?
  • Yes I'm going to publish the xaml, But I don't have any code behind.
  • Level3.StrDescripcionConcepto. where is this property?
  • Sorry lost it in my model sample
  • how can you bind ItemsSource to a single object ItemsSource="{Binding Level2}" ? It should be collection
  • The reason is because binding a Datagrid's itemsource to Hashset doesn't generate a collection view that implements IEditableCollectionView. List will generate a ListCollectionView which does implement this interface, which is why it works.
  • This code is auto generated by EF, could this change affect performance. Or other issues on my relational model.? If not why EF define collections as hashet?
  • @nit very tks for your help, now I need to fight with EF generation code, to maintain my classes generated with List instead Hashet. Again very very tks for your help.
  • This clued me into my problem; in my case I was just dumb and had bound the grid to the result of a LINQ query. The solution was simply to add .ToList() to the end of it.
  • This approach worked fine for me. I also added a little "twist" to make it more narrow: MyGrid.BeginningEdit += (sender, args) => args.Cancel = args.EditingEventArgs.OriginalSource is Border;
  • That's a good additional check and I considered at the time, but it wasn't quite necessary for our UI.
  • I've tried that, but i'm having an error in the following line in the xaml : ItemsSource="{Binding Source={StaticResource ModeloItensViewSource}, Converter={StaticResource collectionConverter}}". The error is "value cannot be null. Parameter name: Collection".
  • @Marlon Sorry for the delay, jsut some days of vacation. did you define ModeloItensViewSource properly ? why it is a static resource ?
  • I've just see done that way in some example in the internet. But i have gave up on entity framework for this project. I was having too much errors and problems, and it was too much work for a small project. Maybe later i'll take some time to study it better. But thanks for the attention anyway.