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: 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.
This is my XAML code:
<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"
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:
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:
COMMENT: Someone helped me in Codeproject, from this URL:
So he changed my code to bellow:
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
It worked, but it is not a good solution. Because it has some issues:
Issue 1: It adds two lines to the grid, not one line!
Issue 2: I type something in a cell, then I hit Enter. The data does not save into the cell and cell will be clean like it was before editing.
Issue 3: After editing, if I press TAB, sends this error: System.InvalidOperationException: 'Two-way binding requires Path or XPath.'
Anybody kindly help please?