datagrid wpf 刷新数据_c# – WPF Datagrid-自动刷新
有很多方法可以改善你的上述內容.但這是我會為初學者嘗試的.
下面將在頁面加載時填充您的數據網格,將計時器設置為每60秒打勾一次.當計時器滴答時,它將調用一種方法將數據再次加載到網格中.
//On PageLoad,populate the grid,and set a timer to repeat ever 60 seconds
private void Page_Loaded(object sender,RoutedEventArgs e)
{
try
{
RebindData();
SetTimer();
}
catch (SqlException e)
{
Console.WriteLine(e.Message);
}
}
//Refreshes grid data on timer tick
protected void dispatcherTimer_Tick(object sender,EventArgs e)
{
RebindData();
}
//Get data and bind to the grid
private void RebindData()
{
String selectstatement = "select top 2 ItemID,DOJ from ConsumarTB order by ItemID ";
da = new SqlDataAdapter(selectstatement,con);
ds = new DataSet();
da.Fill(ds);
dataGrid1.ItemsSource = ds.Tables[0].DefaultView;
}
//Set and start the timer
private void SetTimer()
{
DispatcherTimer dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0,60);
dispatcherTimer.Start();
}
總結
以上是生活随笔為你收集整理的datagrid wpf 刷新数据_c# – WPF Datagrid-自动刷新的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 固定假牙哪种好
- 下一篇: c# char unsigned_dll