Stan
Stan

Reputation: 3

Trying To AutoUpdate My Datagrid by Refreshing the Dataset in My WPF Application

I hope someone can help me as I am a real newbie. I have a WPF Application that is bound to an SQL database on my webServer. Information updating the database is done from an automated trading platform. This application is being designed just to monitor the changes in real time. Binding the data from the webserver to the datagrid was no problem. However, I cannot for the life of me figure out how to make the datagrid update in realtime. The closest I could do was add a refresh button which does successfully update the application everytime I click it. However, I would like it to autoupdate on its own whenever the database has automated changes. Can someone please show me how I can modify my code to make this work? I have placed my code below. Thanks!

using System.Windows;
using Microsoft.Windows.Controls.Primitives;
using System.Collections.Generic;
using C1.WPF.DataGrid;
using StylingWPFGrid.ForexDataSetTableAdapters;
using System.Windows.Threading;
using System;

namespace StylingWPFGrid
{ 
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class WPFGrid : Window
{

    private ForexDataSet _infoDataSet = null;
    public ForexDataSet infoDataSet
    {
        get
        {
            if (_infoDataSet == null)
            {
                _infoDataSet = new ForexDataSet();
                infoTableAdapter info = new infoTableAdapter();
                info.Fill(_infoDataSet.info);
            }

            return _infoDataSet;

        }
    }

    private ForexDataSet _tradesDataSet = null;
    public ForexDataSet tradesDataSet
    {
        get
        {
            if (_tradesDataSet == null)
            {
                _tradesDataSet = new ForexDataSet();
                tradesTableAdapter trades = new tradesTableAdapter();
                trades.Fill(_tradesDataSet.trades);
            }
            return _tradesDataSet;
        }
    }
    public WPFGrid()
    {
        InitializeComponent(); this.AccountsDataGrid.ItemsSource = infoDataSet.info;
        InitializeComponent(); this.TradesDataGrid.ItemsSource = tradesDataSet.trades;

    }

    private void QuitBtn_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
    }

    private void RefreshBtn_Click(object sender, RoutedEventArgs e)
    {
        infoTableAdapter info = new infoTableAdapter();
        info.Fill(_infoDataSet.info);

    }

}

}

Upvotes: 0

Views: 1258

Answers (3)

Billy
Billy

Reputation: 2630

The issue is that you don't have anything setting your properties. So nothing is being updated. That is only part of the problem. The other issue is that you will need to impliment the INotifyPropertyChanged interface.

using System.ComponentModel;
public partial class WPFGrid : Window, INotifyPropertyChanged
{

   public event PropertyChangedEventHandler PropertyChanged;

      private void OnPropertyChanged(string propertyName)
      {
         if (PropertyChanged != null)
         {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
         }
      }
}

Then for your properties you should do:

public ForexDataSet tradesDataSet
{
   get{//add your code here}
   set
   {
      _tradesDataSet = value;
      OnPropertyChanged("tradesDataSet");
   }
}  

Also ensure you are using two way binding if you expect your control to update your database.

Another note: You shouldn't require InitializeComponent(); in your constructor twice! It should be the first line in your constructor and the other two should come after.

The other suggestions to set this up on a timer are good ideas also. I would attempt to do that also, unless you have something setting your datasets. backgroundworker is a good example.

Upvotes: 2

tsells
tsells

Reputation: 2771

Your data in this application is not bound to the database. It is bound to the DataSet you created.

private ForexDataSet _infoDataSet = null;
public ForexDataSet infoDataSet

Since this is a client application using a CLR call back function from the server would not be the best solution. I think in this case setting up an "auto refresh" feature would be sufficient. You could use a timer as suggested previously. I would however do this on a back ground thread (Backgroundworker) so you don't affect the UI while polling for changes.

Upvotes: 0

Stephan B
Stephan B

Reputation: 3701

There is no Info from the Database that it has been updated I believe, so you can just use a Timer to "click" the Button every Second. If the DB cannot handle this try to write a small query that scans for the last update and only fetch the data if this is changed.

Upvotes: 0

Related Questions