digimanus[
^] is partially correct in his comment above.
Firstly, for the DataGrid to see the collection of employees, the DataBinding needs to see the collection as a property of the MainWindow:
Public Property empList = New ObservableCollection(Of Employee)
Next, you will nee a bunch of DataBinding errors in the Debug Output window. Here is one as an example:
System.Windows.Data Error: 40 : BindingExpression path error: 'EmpNo' property not found on 'object' ''Employee' (HashCode=62835574)'. BindingExpression:Path=EmpNo; DataItem='Employee' (HashCode=62835574); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
This is because DataBinding is looking for
Properties
and not
Fields
. So the fix is quite simple:
Public Class Employee
Public Property EmpNo As String
Public Property EmpName As String
Public Property Salary As String
Public Property DeptName As String
End Class
The next problem is that you will see the items loaded twice in the DataGrid. The reason for this is that you are listening to the
Windows.Loaded
event both in the XAML and the code behind. You need to select which one to keep. I would recommend removing from the XAML to keep in line with how you are coding your project.
The last issue that you will have is if you update any properties on you
Employee
class, they won't be reflected as the
Employee
class does not implement the
INotifyPropertyChanged
Interface.
So, your corrected project will look like this:
<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"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="272*"/>
<RowDefinition Height="71*"/>
</Grid.RowDefinitions>
<Button Content="Synchronize" Name="btnsync" Grid.Row="1" FontSize="40" FontFamily="Times New Roman" FontWeight="ExtraBold"/>
<DataGrid Name="dgEmp" AutoGenerateColumns="False" ColumnWidth="*">
<DataGrid.Columns>
<DataGridTextColumn Header="EmpNo" Binding="{Binding EmpNo}" ></DataGridTextColumn>
<DataGridTextColumn Header="EmpName" Binding="{Binding EmpName}" ></DataGridTextColumn>
<DataGridTextColumn Header="Salary" Binding="{Binding Salary}" ></DataGridTextColumn>
<DataGridTextColumn Header="DeptName" Binding="{Binding DeptName}" ></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
and the code-behind:
Imports System.Collections.ObjectModel
Class MainWindow
Public Property empList = New ObservableCollection(Of Employee)
Private Sub btnsync_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnsync.Click
End Sub
Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
empList.Add(New Employee() With {.EmpNo = "2", .EmpName = "Avinash", .Salary = "5000", .DeptName = "Dev"})
empList.Add(New Employee() With {.EmpNo = "3", .EmpName = "Avinash", .Salary = "5000", .DeptName = "Dev"})
empList.Add(New Employee() With {.EmpNo = "4", .EmpName = "Avinash", .Salary = "5000", .DeptName = "Dev"})
dgEmp.ItemsSource = empList
End Sub
Private Sub dgEmp_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs) Handles dgEmp.SelectionChanged
End Sub
End Class
Public Class Employee
Public Property EmpNo As String
Public Property EmpName As String
Public Property Salary As String
Public Property DeptName As String
End Class
I would recommend getting familiar with DataBinding system used in WPF:
Microsoft Docs: Data Binding (WPF)[
^]