开发者

Live data on datagrid for newly added or deleted row in database

开发者 https://www.devze.com 2023-03-10 02:44 出处:网络
public void RefreshEntities(object sender) { for (; ; ) { try { //refreshing entities will not add new rows added so datagrid needs to be recreated
    public void RefreshEntities(object sender)
    {
        for (; ; )
        {
            try
            {
                //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.S开发者_开发知识库IGNILINX_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.");
            }
            Thread.Sleep(300);
        }
    }

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.


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

http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx

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

http://www.dreamincode.net/forums/topic/156991-using-sqldependency-to-monitor-sql-database-changes/

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消