locked
How to bind DataGrid to ObservableCollection and make it dynamic RRS feed

  • Question

  • Hi,

    I would like to bind my Datagrid to ObservableCollection from Entity (class created using ADO.NET Entity Data Model). Now, I have button bind to the command on my ViewModel, but I would like to populate DataGrid automatically and if there are any changes made to the table DataGrid will be updated as well.

        Public Property UserDetails() As ObservableCollection(Of tbl_Main_Timer)
            Get
                Return _UserDetails
            End Get
            Set(value As ObservableCollection(Of tbl_Main_Timer))
                _UserDetails = value
            End Set
        End Property
    
        Public Property FillDataGrid() As RelayCommand
            Get
                _FillDatagrid = New RelayCommand(AddressOf FillProductivityEE)
                Return _FillDatagrid
            End Get
            Set(value As RelayCommand)
                _FillDatagrid = value
                RaisePropertyChanged("FillDataGrid")
            End Set
        End Property
    
        Public Sub FillProductivityEE()
            Using entityconn As New TimerConn()
                Dim result = (From f In entityconn.tbl_Main_Timer Where f.Enumber = Enumber AndAlso f.LogDate = "2015-07-01" Order By f.ID).ToList
                For Each ac In result
                    _UserDetails.Add(ac)
                Next
            End Using
        End Sub

    DataGrid ItemsSource:

    ItemsSource="{Binding EmployeeViewM.UserView}" 

    Should I use CollectionViewSource here? 

    Thank you very much.

    Thursday, July 2, 2015 4:49 AM

Answers

  • The object context and the collection on your computer have no live link to the database.
    When you ask for data EF will go read that data for you.

    Telling the client that there have been changes on the server is quite a complication.

    You need to work out that there have been changes to the data the client has for a start.

    There is a built in way to do that, but it's inefficient so unsuitable for large scale scenarios.

    https://msdn.microsoft.com/en-us/library/62xk7953%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    You will notice that this is not entity framework friendly.

    A simpler and yet more inefficient approach is to poll.

    You read the stuff again every 30 seconds or whatever.

    More efficient but more work is to use triggers on your tables to track which records change. They write to a changes table with Table, Id, change type, timestamp and user.

    You can then query against that to find what's changed since you read data.

    You can do that from the client.

    You can use a business server to poll every minute or something and then tell clients what's changed in that minute using SignalR.

    By the way.

    Have you considered what happens if the user is editing a record that has changed as they edit?

    .

    It's a complex subject.


    Hope that helps.

    Technet articles: WPF: Change Tracking; All my Technet Articles

    • Marked as answer by VoVoTT Friday, July 3, 2015 6:17 AM
    Thursday, July 2, 2015 6:40 AM
  • Do you mean that you want the DataGrid to display any new rows or updated data that you add to the database from somewhere else? Then you have to either fetch the data from the database again at regular intervals - you could for example use a Timer: https://csharptips.wordpress.com/tag/system-threading-timer/ - or use some kind of pushing mechanism, for example the SqlDependency class: https://msdn.microsoft.com/en-us/library/a52dhwx7(v=vs.80).aspx.

    How to push data to a client application or how to fetch data from a database is not a WPF topic though so please start a new thread in an appropriate forum if you have any more questions about how to do this.

    As far as WPF is concerned, it has no two-way connection to the underlying database and it cannot listen for changes made in the database out of the box. You query the data through your context and basically get a snapshot of the data.

    To save in the DataGrid back to the database, you call the SaveChanges() method on the same context, e.g.:

    Dim TimerConn entityconn As New TimerConn()
    Public Sub FillProductivityEE()
                Dim result = (From f In entityconn.tbl_Main_Timer Where f.Enumber = Enumber AndAlso f.LogDate = "2015-07-01" Order By f.ID).ToList
                For Each ac In result
                    _UserDetails.Add(ac)
                Next
    End Sub
    
    Public Sub Save()
                entityConn.SaveChanges()
    End Sub
    

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question.

    • Proposed as answer by Xavier Xie-MSFT Friday, July 3, 2015 1:38 AM
    • Marked as answer by VoVoTT Friday, July 3, 2015 6:16 AM
    Thursday, July 2, 2015 8:03 AM

All replies

  • The object context and the collection on your computer have no live link to the database.
    When you ask for data EF will go read that data for you.

    Telling the client that there have been changes on the server is quite a complication.

    You need to work out that there have been changes to the data the client has for a start.

    There is a built in way to do that, but it's inefficient so unsuitable for large scale scenarios.

    https://msdn.microsoft.com/en-us/library/62xk7953%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    You will notice that this is not entity framework friendly.

    A simpler and yet more inefficient approach is to poll.

    You read the stuff again every 30 seconds or whatever.

    More efficient but more work is to use triggers on your tables to track which records change. They write to a changes table with Table, Id, change type, timestamp and user.

    You can then query against that to find what's changed since you read data.

    You can do that from the client.

    You can use a business server to poll every minute or something and then tell clients what's changed in that minute using SignalR.

    By the way.

    Have you considered what happens if the user is editing a record that has changed as they edit?

    .

    It's a complex subject.


    Hope that helps.

    Technet articles: WPF: Change Tracking; All my Technet Articles

    • Marked as answer by VoVoTT Friday, July 3, 2015 6:17 AM
    Thursday, July 2, 2015 6:40 AM
  • Do you mean that you want the DataGrid to display any new rows or updated data that you add to the database from somewhere else? Then you have to either fetch the data from the database again at regular intervals - you could for example use a Timer: https://csharptips.wordpress.com/tag/system-threading-timer/ - or use some kind of pushing mechanism, for example the SqlDependency class: https://msdn.microsoft.com/en-us/library/a52dhwx7(v=vs.80).aspx.

    How to push data to a client application or how to fetch data from a database is not a WPF topic though so please start a new thread in an appropriate forum if you have any more questions about how to do this.

    As far as WPF is concerned, it has no two-way connection to the underlying database and it cannot listen for changes made in the database out of the box. You query the data through your context and basically get a snapshot of the data.

    To save in the DataGrid back to the database, you call the SaveChanges() method on the same context, e.g.:

    Dim TimerConn entityconn As New TimerConn()
    Public Sub FillProductivityEE()
                Dim result = (From f In entityconn.tbl_Main_Timer Where f.Enumber = Enumber AndAlso f.LogDate = "2015-07-01" Order By f.ID).ToList
                For Each ac In result
                    _UserDetails.Add(ac)
                Next
    End Sub
    
    Public Sub Save()
                entityConn.SaveChanges()
    End Sub
    

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question.

    • Proposed as answer by Xavier Xie-MSFT Friday, July 3, 2015 1:38 AM
    • Marked as answer by VoVoTT Friday, July 3, 2015 6:16 AM
    Thursday, July 2, 2015 8:03 AM
  • Thank you. Yesterday I saw this: http://dynamic-data.org/ (might be helpful for someone).

    I'll play with SqlDependency. 

    Thank you

    Friday, July 3, 2015 6:16 AM