自动刷新-WPF Datagrid [英] Auto refresh -WPF Datagrid
问题描述
我有一个数据网格,显示一个绑定到sql server DB的表。
我想每隔60秒设置一个Timer,检查任何更新,然后显示最新的更新的数据。
到目前为止,我为datagrid创建了一个event_handler,其中包括对象dispatchertimer
I have a datagrid that displays a table which is bound to a sql server DB.
I would like to set a Timer for every 60 sec, that checks for any updation and then displays the latest updated data.
So far I have created a event_handler for datagrid, that includes the object dispatchertimer
private void dataGrid1_loaded(object sender, RoutedEventArgs e)
{
DispatcherTimer dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 60);
dispatcherTimer.Start();
}
现在我不知道如何继续使用事件处理程序来处理来自的最新数据数据库。
Now I don''t know how to proceed further with the event handler to handle the newly updated data from the database.
dispatcherTimer_Tick
这是我的select语句,用于填充数据网格。
here is my select statement that is used to fill the datagrid.
private void Page_Loaded(object sender, RoutedEventArgs e)
{
try
{
String selectstatement = "select top 2 ItemID, ItemName,ConsumerName, Street, DOJ from ConsumarTB order by ItemID ";
da = new SqlDataAdapter(selectstatement, con);
ds = new DataSet();
da.Fill(ds);
dataGrid1.ItemsSource = ds.Tables[0].DefaultView;
}
catch (SqlException e)
{
Console.WriteLine(e.Message);
}
}
即使有其他方法,请提供帮助。
谢谢!
Kindly help, even if there is another approach.
Thanks!
推荐答案
您每60秒从db加载数据并将其与本地副本进行比较,如果有任何更改,您将需要手动为每一行更改值根据new-one(例如通过表键,假设您有Id等于1的人,并且在您加载数据到Wpf应用程序之后的某个更改他的名称,因此从DB获取数据密钥从Db获取数据并比较值,如果有任何更改确实更新!!)
一个警告,这种方法可能导致耗时的操作,所以在单独的操作线程。
我不认为这是一个非常好的解决方案......
You have every 60 sec load data from db and compare it with you local copy , in case of any changes , you will need manually for each row change values according to new-one (for example by table key, lets assume that you have person with Id equals 1, and some one AFTER YOU LOAD DATA to Wpf app change his name , so get data from DB for each key fetch data from Db and compare values , if any changes do update!!)
One warning , this approach can lead to time consuming op, so perfom it in separate thread.
And i do not think that is so quite good solution...
这篇关于自动刷新-WPF Datagrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!