DevExpress GridControl即使我正确设置了NotifyPropertyChanged事件,也无法正确更新

时间:2012-11-05 12:26:05

标签: devexpress gridcontrol

我遇到了一个非常奇怪的问题。

基本的想法是,我有一个班级来保存从交易api收到的有关外汇价格的数据。每个属性都使用NotifyPropertyChanged方法设置,如下所示。

class RealTimeBar
{
    public event PropertyChangedEventHandler PropertyChanged;

    private const double EPSILON = 0.0000001;

    private int _id;
    private string _symbol;
    private int _time;
    private float _open;
    private float _high;
    private float _low;
    private float _close;
    int _volume;

    public RealTimeBar(int id, string symbol)
    {
        _id = id;
        _symbol = symbol;
    }

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    public int Id
    {
        get
        {
            return _id;
        }
        set
        {
            _id = value;
        }
    }

    public string Symbol
    {
        get
        {
            return _symbol;
        }
        set
        {
            if (value != _symbol)
            {
                _symbol = value;
                NotifyPropertyChanged("Symbol");
            }
        }
    }

    public int Time
    {
        get
        {
            return _time;
        }
        set
        {
            if (value != _time)
            {
                _time = value;
                NotifyPropertyChanged("Time");
            }
        }
    }

    public float Open
    {
        get
        {
            return _open;
        }
        set
        {
            if (value != _open)
            {
                _open = value;
                NotifyPropertyChanged("Open");
            }
        }
    }

    public float High
    {
        get
        {
            return _high;
        }
        set
        {
            if (value != _high)
            {
                _high = value;
                NotifyPropertyChanged("High");
            }
        }
    }

    public float Low
    {
        get
        {
            return _low;
        }
        set
        {
            if (value != _low)
            {
                _low = value;
                NotifyPropertyChanged("Low");
            }
        }
    }

    public float Close
    {
        get
        {
            return _close;
        }
        set
        {
            if (value != _close)
            {
                _close = value;
                NotifyPropertyChanged("Close");
            }
        }
    }

    public int Volume
    {
        get
        {
            return _volume;
        }
        set
        {
            if (value != _volume)
            {
                _volume = value;
                NotifyPropertyChanged("Volume");
            }
        }
    }


}

它引用了一个长类,但结构简单,你可以看到。现在我连接到api哪个fire事件给我,我通过将api中的值设置为我定义的类来处理它。

    BindingList<RealTimeBar> _realTimeBarList = new BindingList<RealTimeBar>();
    public Hashtable _iForexHashtable = new Hashtable();

    private void _UpdateForexQuote(int tickerId, int time, double open, double high, double         low,    double close, int volume,
                             double wap, int count)
    {
        ///MessageBox.Show(tickerId.ToString());
        ((RealTimeBar)_iForexHashtable[tickerId]).Open = (float)open;
        ((RealTimeBar)_iForexHashtable[tickerId]).High = (float)high;
        ((RealTimeBar)_iForexHashtable[tickerId]).Low = (float)low;
        ((RealTimeBar)_iForexHashtable[tickerId]).Close = (float)close;
        ((RealTimeBar)_iForexHashtable[tickerId]).Volume = volume;

    }

经过一些设置后,方法_UpdateForexQuote会将即将发布的信息分发到RealTimeBar类的属性中。一切都好。

当我启动程序时,它不会更新。我认为没有数据进入。但是当我随机点击gridcontrol的A1cell中的某个地方,然后点击另一个B1cell时,之前的A1cell将更新。然后,如果我单击C1cell,则B1cell将更新。如果您没有单击一个单元格,它将永远不会更新。我告诉你这张照片:

enter image description here

正如您所看到的,在点击前三行之后,前三行显示延迟数据,因为我从未触及第四行,所以它显示为零。而条件是我只是点击了第五行Low cell,这就是Low不更新但其他单元格更新的原因。这很奇怪。我在devexpress 11和vs 2010之前使用相同的代码。但是现在使用devexpress 12和vs 2012,我遇到了以前从未发生的这个问题。

更新

下面是我用来定义bindinglist和散列表的方法,2。首先将对象放入散列表,然后将对象从散列表添加到bindinglist 3.将绑定列表绑定到gridcontrol。

private void earningButtonItem_ItemClick(object sender, ItemClickEventArgs e)
    {
        _iTimer.AutoReset = false;
        _iTimer.Enabled = false;
        switchStockPool = "Earning Stock";
        disconnectButtonItem.PerformClick();
        connectButtonItem.PerformClick();
        _iheitanshaoEarningDBConnect = new DBConnect("heitanshaoearning");
        List<string>[] tempList;
        int tempHash;
        tempList = _iheitanshaoEarningDBConnect.SelectSymbolHighLow();
        _quoteEarningOnGridList.Clear();

        ///tempList[0].Count
        for (int i = 0; i < tempList[0].Count; i++)
        {
            tempHash = Convert.ToInt32(tempList[0][i].ToString().GetHashCode());
            _iStockEarningHistHashtable[tempHash] = new QuoteOnGridHist(tempList[0][i], (float)Convert.ToSingle(tempList[1][i]), (float)Convert.ToSingle(tempList[2][i]), (float)Convert.ToSingle(tempList[3][i]));
            _iStockEarningHashtable[tempHash] = new QuoteOnGrid(tempList[0][i], 0, 0);
            _quoteEarningOnGridList.Add((QuoteOnGrid)_iStockEarningHashtable[tempHash]);
            reqMktDataExStock(tempHash, tempList[0][i].ToString());
        }

        List<string>[] tempVolumeList;
        tempVolumeList = _iheitanshaoEarningDBConnect.SelectAverageVolume();
        for (int i = 0; i < tempList[0].Count; i++)
        {
            tempHash = Convert.ToInt32(tempVolumeList[0][i].ToString().GetHashCode());
            ((QuoteOnGrid)_iStockEarningHashtable[tempHash]).Average_Volume = ((float)Convert.ToSingle(tempVolumeList[1][i])) / volumeDenominator;
        }

        gridControl.DataSource = _quoteEarningOnGridList;
    }
    /////////////////////

现在,当价格更新事件到来时,下面的方法将更新哈希表中的对象属性。由于我在对象中定义了Notifypropertychanged,它应该更新bingdinglist和gridcontrol中的对象。

  private void _UpdateStockMarketQuote(int tikcerId, int field, double price, int canAutoExecute)
    {
        ////MessageBox.Show(tikcerId.ToString() + field.ToString() + price.ToString());
        if (switchStockPool == "Selected Stock")
        {
            if (field == 4)
            {
                ((QuoteOnGrid)_iStockHashtable[tikcerId]).Gap_From_High = ((float)price - ((QuoteOnGridHist)_iStockHistHashtable[tikcerId]).High) / ((QuoteOnGridHist)_iStockHistHashtable[tikcerId]).Close;
                ((QuoteOnGrid)_iStockHashtable[tikcerId]).Gap_From_Low = ((float)price - ((QuoteOnGridHist)_iStockHistHashtable[tikcerId]).Low) / ((QuoteOnGridHist)_iStockHistHashtable[tikcerId]).Close;
                ((QuoteOnGrid)_iStockHashtable[tikcerId]).Last_Price = (float)price;
            }
            //else if (field == 1)
            //{
            //    ((QuoteOnGrid)_iStockHashtable[tikcerId]).Gap_From_High = ((float)price - ((QuoteOnGridHist)_iStockHistHashtable[tikcerId]).High) / ((QuoteOnGridHist)_iStockHistHashtable[tikcerId]).Close;
            //    ((QuoteOnGrid)_iStockHashtable[tikcerId]).Gap_From_Low = ((float)price - ((QuoteOnGridHist)_iStockHistHashtable[tikcerId]).Low) / ((QuoteOnGridHist)_iStockHistHashtable[tikcerId]).Close;
            //}
        }
        else if (switchStockPool == "Earning Stock")
        {
            if (field == 4)
            {
                ((QuoteOnGrid)_iStockEarningHashtable[tikcerId]).Gap_From_High = ((float)price - ((QuoteOnGridHist)_iStockEarningHistHashtable[tikcerId]).High) / ((QuoteOnGridHist)_iStockEarningHistHashtable[tikcerId]).Close;
                ((QuoteOnGrid)_iStockEarningHashtable[tikcerId]).Gap_From_Low = ((float)price - ((QuoteOnGridHist)_iStockEarningHistHashtable[tikcerId]).Low) / ((QuoteOnGridHist)_iStockEarningHistHashtable[tikcerId]).Close;
                ((QuoteOnGrid)_iStockEarningHashtable[tikcerId]).Last_Price = (float)price;

            }
            //else if (field == 1)
            //{
            //    ((quoteongrid)_istockearninghashtable[tikcerid]).gap_from_high = ((float)price - ((quoteongridhist)_istockearninghisthashtable[tikcerid]).high) / ((quoteongridhist)_istockearninghisthashtable[tikcerid]).close;
            //    ((quoteongrid)_istockearninghashtable[tikcerid]).gap_from_low = ((float)price - ((quoteongridhist)_istockearninghisthashtable[tikcerid]).low) / ((quoteongridhist)_istockearninghisthashtable[tikcerid]).close;
            //}
        }

    }

1 个答案:

答案 0 :(得分:0)

您不仅需要在课程中拥有PropertyChanged个事件,还需要实施INotifyPropertyChanged。这就是网格知道一个类可以告知变化的方式。