Take the 2-minute tour ×
    public void RefreshEntities(object sender)
        for (; ; )
                //refreshing entities will not add new rows added so datagrid needs to be recreated
                if (isChanged())
                    Fetch(); //Reset binding or reconstruct data grid
                    networkEntityTracker = pLCSignilinxEntities.SIGNILINX_NETWORK.ToList().Count;
                    systemEntityTracker = pLCSignilinxEntities.SIGNILINX_SYSTEM.ToList().Count;
                    tagEntityTracker = pLCSignilinxEntities.SIGNILINX_TAG.ToList().Count;
                pLCSignilinxEntities.Refresh(System.Data.Objects.RefreshMode.StoreWins, pLCSignilinxEntities.SIGNILINX_NETWORK);
                pLCSignilinxEntities.Refresh(System.Data.Objects.RefreshMode.StoreWins, pLCSignilinxEntities.SIGNILINX_SYSTEM);
                pLCSignilinxEntities.Refresh(System.Data.Objects.RefreshMode.StoreWins, pLCSignilinxEntities.SIGNILINX_TAG);
            catch (Exception ex)
                MessageBox.Show("Unable to refresh.");

I have a datagrid dragged from Data Sources. The datagrid corresponds to a table of an entity data model created from SQL database. The table in the database changes regularly. So the goal of my program is to show live data on the datagrid. And my program is working very well displaying live data except for new or deleted rows. When a new row is inserted into the table in SQL database or a row is delete is deleted, the datagrid does not update that row.

The above function is the function that refreshes the data grid. I think I have to write the Fetch() function to reset binding or reconstruct data grid in order to update the data grid for newly added row or deleted row.

Then this function is called by background thread as delegate so that the data grid is refreshed every 300 ms.

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

First thing a better approach would be to Threading.Timer instead of the infinite loop that sleeps.


Or if you have access you could setup a Sql Dependency to fire a change event every time data is updated, added or deleted. Though for me this has never worked very well, and I ended up just rolling my own. But here is some stuff on the sql dependency


Then you just need to make sure you are not caching anything in memory and reloading the data from the database table each time. If you do not want to pull all an entire table (which could take awhile depending on the size) you can always setup so some sort of change table that keeps track of what records were updated, added or deleted since the last time you checked and then just pull the data for those records or remove from memory the ones that were deleted.

Hopefully this helps you out a bit.

share|improve this answer
add comment

Your Answer


