Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I wrote a simple wpf datagrid. So when I double click on a cell, the program crashes. I should add that I checked this post: https://stackoverflow.com/questions/19334326/wpf-datagrid-edititem-is-not-allowed-for-this-view-exception[^] But there was not an answer for my question. The answer there is for datagrid which is in windows form.

What I have tried:

This is my XAML code:
XML
<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp4"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid Margin="10">
        <DataGrid x:Name="myDGList" 
                  AutoGenerateColumns="False"
                  HorizontalContentAlignment="Stretch"
                  VerticalContentAlignment="Stretch"
                  FontFamily="Tahoma"
                  FontSize="12"
                  IsReadOnly="False"
                  GridLinesVisibility="All"
                  CanUserAddRows="True"
                  CanUserDeleteRows="True">

            <DataGrid.Columns>
                <DataGridTextColumn Header="Number" IsReadOnly="False" Width="100" Binding="{Binding employeeID}"/>
                <DataGridTextColumn Header="Name" IsReadOnly="False" Width="100" Binding="{Binding employeeName}"/>
                <DataGridTextColumn Header="Address" IsReadOnly="False" Width="*" Binding="{Binding employeeAddress}"/>
                <DataGridTextColumn Header="City" IsReadOnly="False" Width="*" Binding="{Binding employeeCity}"/>
                <DataGridTextColumn Header="State" IsReadOnly="False" Width="*" Binding="{Binding employeeState}"/>
            </DataGrid.Columns>
        </DataGrid>


This is the MainWindow code:
XML
Class MainWindow
    Sub New()
        InitializeComponent()
        Dim JohnSmith As Employee = New Employee
        JohnSmith.employeeID = "001"
        JohnSmith.employeeName = "John smith"
        JohnSmith.employeeAddress = "122 made up Lane"
        JohnSmith.employeeCity = "cameden"
        JohnSmith.employeeState = "New Jersy"
        myDGList.Items.Add(JohnSmith)
    End Sub
    Partial Class Employee
        Public Property employeeID As String
        Public Property employeeName As String
        Public Property employeeAddress As String
        Public Property employeeCity As String
        Public Property employeeState As String
    End Class
End Class


So after I double click on a cell, program crashes and bellow page opens in visual studio:
System.InvalidOperationException:
'EditItem' is not allowed for this view
Posted
Updated 24-Jun-23 21:49pm
v5
Comments
Richard Deeming 20-Jun-23 14:23pm    
Don't post screenshots of your error messages. That error dialog has a "copy details" link which will copy the full exception details to the clipboard in text.

I was able to reproduce the problem. As far as I can see, it's due to the fact that you're adding items directly to the DataGrid's Items collection.

If I change your code to add the employee to a List(Of Employee) instead, and assign that list to the DataGrid's ItemSource property, then the code works as expected. You will be able to edit the existing row, and add new rows.
VB.NET
Sub New()
    InitializeComponent()
    
    Dim JohnSmith As Employee = New Employee
    JohnSmith.employeeID = "001"
    JohnSmith.employeeName = "John smith"
    JohnSmith.employeeAddress = "122 made up Lane"
    JohnSmith.employeeCity = "cameden"
    JohnSmith.employeeState = "New Jersy"
    
    Dim employeeList As New System.Collections.Generic.List(Of Employee)
    employeeList.Add(JohnSmith)
    
    myDGList.ItemsSource = employeeList
End Sub
 
Share this answer
 
Comments
Sh.H. 25-Jun-23 3:03am    
Thanks. I tested the code you wrote.
It works, but has an issue.

Issue: There is always an empty row at the bottom of Datagrid.

Please kindly let me know how can I solve these issues?
Richard Deeming 3-Jul-23 10:20am    
If you don't want the "empty" row, then set CanUserAddRows="False"; if you set it to "True", then you get a blank row for the user to enter the details of the new record.
According to this page :
Specify the Edit Mode for DataGridView Control - Windows Forms .NET Framework | Microsoft Learn[^]

You must have
Quote:
The ReadOnly properties of the cell, row, column, and control are all set to false.
 
Share this answer
 
Comments
Richard Deeming 21-Jun-23 3:43am    
Based on the XAML code in the question, all of the columns already have IsReadOnly="False", as does the grid.
Sh.H. 22-Jun-23 9:15am    
Yes. All are already False.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900