Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'EditItem' is not allowed for this view - WPF Data editing issue

Tags:

wpf

datagrid

I have a treeview Students. My items in treeview are names of students in a class. On selection of a item in TreeView, Data in my DataGrid 'StudentDetails' should change. My DataGrid has two columns 'Parameter_Details' and 'Details'. The second column details should be editable. Rows may vary depending on the selection of TreeViewItem.

For example, My TreeView has two items, say
Jack
Jill

When I click/ select Jack, my DataGrid 'Student Details' shows 1 row with two columns String 'Address' under 'Parameter_Details' column and string.Empty under 'Details' columns

Address|

When I click/ select Jill, my DataGrid 'StudentDetails' shows 2 rows with two columns 1st row: String 'Last Name' under 'Parameter_Details' column and string.Empty under 'Details' columns 2nd row: String 'Address' under 'Parameter_Details' column and string.Empty under 'Details' columns Last Name| Address |

I am adding data to my datagrid using foreach loop. I get 'ParameterDetails' from DB where each row has Parameter corresponding to TreeviewItem

Whenever I select the TreeviewItem, I do the attached piece of code:

dgStudentDetails.Items.Clear();

foreach (Parameter_Details entry in ParameterDetails)
{
    if(entry.ID == SelectedTVItem.ID)
    {
        dgKeywordParameters.Items.Add(new Parameter_dgInput() { name = entry.Name, input = "" });
    }
}

The Data gets binded to the datagrid but when I am trying to edit 2nd column using

private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    DataGridCell cell = sender as DataGridCell;

    if (cell != null && !cell.IsEditing && !cell.IsReadOnly)
    {
        // enables editing on single click
        if (!cell.IsFocused)
        cell.Focus();

        DataGrid dataGrid = UIHelpers.TryFindParent<DataGrid>(cell);

       if (dataGrid != null)
        {
             if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow)
             {
                 if (!cell.IsSelected)
                      cell.IsSelected = true;
             }
             else
             {
                  DataGridRow row = UIHelpers.TryFindParent<DataGridRow>(cell);
                  if (row != null && !row.IsSelected)
                  {
                      row.IsSelected = true;
                  }
             }
        }
    }
}

I get the following error:

System.InvalidOperationException was unhandled
  Message='EditItem' is not allowed for this view.
  Source=PresentationFramework
  StackTrace:
       at System.Windows.Controls.ItemCollection.System.ComponentModel.IEditableCollectionView.EditItem(Object item)
       at System.Windows.Controls.DataGrid.EditRowItem(Object rowItem)
       at System.Windows.Controls.DataGrid.OnExecutedBeginEdit(ExecutedRoutedEventArgs e)
       at System.Windows.Controls.DataGrid.OnExecutedBeginEdit(Object sender, ExecutedRoutedEventArgs e)
       at System.Windows.Input.CommandBinding.OnExecuted(Object sender, ExecutedRoutedEventArgs e)
       at System.Windows.Input.CommandManager.ExecuteCommandBinding(Object sender, ExecutedRoutedEventArgs e, CommandBinding commandBinding)
       at System.Windows.Input.CommandManager.FindCommandBinding(CommandBindingCollection commandBindings, Object sender, RoutedEventArgs e, ICommand command, Boolean execute)
       at System.Windows.Input.CommandManager.FindCommandBinding(Object sender, RoutedEventArgs e, ICommand command, Boolean execute)
       at System.Windows.Input.CommandManager.OnExecuted(Object sender, ExecutedRoutedEventArgs e)
       at System.Windows.UIElement.OnExecutedThunk(Object sender, ExecutedRoutedEventArgs e)
       at System.Windows.Input.ExecutedRoutedEventArgs.InvokeEventHandler(Delegate genericHandler, Object target)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
       at System.Windows.Input.RoutedCommand.ExecuteImpl(Object parameter, IInputElement target, Boolean userInitiated)
       at System.Windows.Input.RoutedCommand.Execute(Object parameter, IInputElement target)
       at System.Windows.Controls.DataGrid.BeginEdit(RoutedEventArgs editingEventArgs)
       at System.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDown(MouseButtonEventArgs e)
       at System.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDownThunk(Object sender, MouseButtonEventArgs e)
       at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
       at System.Windows.UIElement.OnMouseDownThunk(Object sender, MouseButtonEventArgs e)
       at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
       at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
       at System.Windows.Input.InputManager.ProcessStagingArea()
       at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
       at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
       at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
       at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       at System.Windows.Application.RunDispatcher(Object ignore)
       at System.Windows.Application.RunInternal(Window window)
       at System.Windows.Application.Run(Window window)
       at System.Windows.Application.Run()
       at EBS.App.Main() in C:\projects\EBS\EBS\obj\x86\Debug\App.g.cs:line 0
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:
like image 211
Priyanka Avatar asked Dec 30 '11 08:12

Priyanka


2 Answers

I have had this error too. It seems there are many possible causes for the same error message.

I think in your case it may work to do what I did. I think the problem is that DataGrid will throw this error when editing data if the bound data is not one of the types that it will allow editing upon.

What seems to work well for me, is if I create a new List of whatever I want to show (perhaps a class I make for that purpose, where I define each column in the grid), and then bind the DataGrid to that list.

You can see my Q&A about this at: Why does one of MY WPF DataGrids give the "'EditItem' is not allowed for this view" exception?

like image 130
Dronz Avatar answered Nov 18 '22 05:11

Dronz


My answer from Why does one of MY WPF DataGrids give the "'EditItem' is not allowed for this view" exception? boils down to using an ObservableCollection as the bound item: https://stackoverflow.com/a/36924136/4504897 (Linked to avoid duping answers; not sure what the etiquette is for that; perhaps SO needs a way of adding existing answers to a question?)

like image 1
VaelynPhi Avatar answered Nov 18 '22 04:11

VaelynPhi