当单击datagrid行时,它抛出异常-WPF C#

当单击datagrid行时,它抛出异常-WPF C#,c#,wpf,datagrid,C#,Wpf,Datagrid,每当我试图双击我的datagrid行来编辑它时,我抛出一些异常,这些异常对我来说没有任何意义。如果我将整个datagrid设置为IsReadOnly为true,我希望出现问题,但我需要第二列和第三列可编辑 XAML 例外情况 Exception:Thrown: "'EditItem' is not allowed for this view."(System.InvalidOperationException) Exception:Thrown: "The string was not rec

每当我试图双击我的datagrid行来编辑它时,我抛出一些异常,这些异常对我来说没有任何意义。如果我将整个datagrid设置为IsReadOnly为true,我希望出现问题,但我需要第二列和第三列可编辑

XAML

例外情况

Exception:Thrown: "'EditItem' is not allowed for this view."(System.InvalidOperationException)

Exception:Thrown: "The string was not recognized as a valid DateTime. There is an unknown word starting at index 0." (System.FormatException)

您应该使用
ObservableCollection()
作为数据网格的源,而不是将项目直接添加到
Datagrid
。为此,您需要创建一个属性
observedcollection Data
,并将其用作
DataGrid
中的
ItemsSource
,如下所示
ItemsSource=“{Binding MyBikesOrders}”
。然后,您可以将项目添加到
可观察集合
,更新
数据网格
,您应该能够直接在其中编辑条目

这是一个与您的问题非常相似的问题。

可能重复:
clientList.Items.Add(new DataClients { Id = 1, Company = "My Company", Name = "Jane Roe"});
Exception:Thrown: "'EditItem' is not allowed for this view."(System.InvalidOperationException)

Exception:Thrown: "The string was not recognized as a valid DateTime. There is an unknown word starting at index 0." (System.FormatException)