Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
xaml :
<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    Loaded="Window_Loaded">
    <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>



MainWindow.xaml.vb code
Imports System.Collections.ObjectModel

Class MainWindow

Public Class Employee
Public EmpNo As String
Public EmpName As String
Public Salary As String
Public DeptName As String
End Class

Dim empList As New ObservableCollection(Of Employee)

Private Sub btnsync_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnsync.Click
'empList.Add(New Employee() With {.EmpNo = "2", .EmpName = "Avinash", .Salary = "5000", .DeptName = "Dev"})
'dgEmp.Items.Refresh()
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

What I have tried:

I verified, Items source is having data but it is not shown on dataGrid.
Posted
Updated 19-Mar-18 12:49pm
Comments
Herman<T>.Instance 19-Mar-18 17:21pm    
I do not see ItemsSource={Binding ...} in your XAML,neither a SelectedItem={Binding ....}
Graeme_Grant 19-Mar-18 18:16pm    
That's because he is not using XAML DataBinding, he's hard coding in the code-behind. See:
 dgEmp.ItemsSource = empList

1 solution

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:
VB
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:
VB
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:
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"
        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:
VB
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
        'empList.Add(New Employee() With {.EmpNo = "2", .EmpName = "Avinash", .Salary = "5000", .DeptName = "Dev"})
        'dgEmp.Items.Refresh()
    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)[^]
 
Share this answer
 
v2

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