DataGrid Not Being Populated

RogerSchlueter-7899 1,446 Reputation points
2023-06-27T01:19:11.16+00:00

Consider this class:

Public Class Transaction
    Implements INotifyPropertyChanged

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    << Various properties >>
End Class

and this:

Public Class TransactionEx
    Inherits Transaction
    << Additional properties >>
End Class

In code-behind, I load data from a database (I have verified this is working correctly) into

Private _TransactionsEx As New ObservableCollection(Of TransactionEx)
Public Property TransactionsEx As ObservableCollection(Of TransactionEx)
    Get
        Return _TransactionsEx
    End Get
    Set(value As ObservableCollection(Of TransactionEx))
        _TransactionsEx = value
    End Set
End Property

The Property TransactionsEx serves as the ItemsSource for a DataGrid, however, no data appears in the DataGrid. I think the problem might be that the CollectionChanged event is not triggered so the DataGrid doesn't know that there is data to display.

If that is the case, what do I have to do trigger that event. Or do you see any other issue?

Developer technologies Windows Presentation Foundation
Developer technologies VB
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
    2023-06-27T05:56:07.0366667+00:00

    Hi,@Roger Schlueter. Welcome Microsoft Q&A.

    I add data to the collection to test it can successfully populate the DataGrid. You could try to refer to the code below and check your code. Please let me know if there is any problem.

    <Window.DataContext>
            <local:MainWindowVM/>
        </Window.DataContext>
        <Grid>
            <DataGrid Width="400" Height="300" ItemsSource="{Binding TransactionsEx }"/>
        </Grid>
    
    Public Class MainWindowVM
    
        Private _TransactionsEx As New ObservableCollection(Of TransactionEx)
        Public Property TransactionsEx As ObservableCollection(Of TransactionEx)
            Get
                Return _TransactionsEx
            End Get
            Set(value As ObservableCollection(Of TransactionEx))
                _TransactionsEx = value
            End Set
        End Property
    
    
    
        Public Sub New()
            _TransactionsEx.Add(New TransactionEx() With {
                .Id = 1,
                .Name = "user1",
                .Detail = "detail1"
            })
            _TransactionsEx.Add(New TransactionEx() With {
                .Id = 2,
                .Name = "user2",
                .Detail = "detail2"
            })
            _TransactionsEx.Add(New TransactionEx() With {
                .Id = 3,
                .Name = "user3",
                .Detail = "detail3"
            })
    
        End Sub
    
    
    End Class
    Public Class TransactionEx
        Inherits Transaction
    
    
        Private _detail As String
    
        Public Property Detail As String
            Get
                Return _detail
            End Get
            Set(ByVal value As String)
    
    
                Me._detail = value
                NotifyPropertyChanged("Detail")
    
            End Set
        End Property
    
    
    
    
    End Class
    
    Public Class Transaction
        Implements INotifyPropertyChanged
    
    
    
        Private _id As Integer
    
        Public Property Id As Integer
            Get
                Return _id
            End Get
            Set(ByVal value As Integer)
    
    
                Me._id = value
                NotifyPropertyChanged("Id")
    
            End Set
        End Property
    
        Private _name As String
    
        Public Property Name As String
            Get
                Return _name
            End Get
            Set(ByVal value As String)
    
    
                Me._name = value
                NotifyPropertyChanged("Name")
    
            End Set
        End Property
        Public Event PropertyChanged As PropertyChangedEventHandler _
                Implements INotifyPropertyChanged.PropertyChanged
    
        Public Sub NotifyPropertyChanged(ByVal propertyName As String)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
        End Sub
    End Class
    
    
    
    

    The result:

    User's image


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.