1

我有以下 POCO 对象:

    internal class StationEntity : INotifyPropertyChanged
{
    private int _id;
    private string _stationType;
    private string _stationName;

    [Obfuscation(Exclude = true)]
    public int ID
    {
        get { return _id; }
        set
        {
            if (value == _id) return;
            _id = value;
            OnPropertyChanged("ID");
        }
    }

    [Obfuscation(Exclude = true)]
    public string StationType
    {
        get { return _stationType; }
        set
        {
            if (value == _stationType) return;
            _stationType = value;
            OnPropertyChanged("StationType");
        }
    }

    [Obfuscation(Exclude = true)]
    public string StationName
    {
        get { return _stationName; }
        set
        {
            if (value == _stationName) return;
            _stationName = value;
            OnPropertyChanged("StationName");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

我将网格的数据源设置为 BindingSource,并将绑定源设置如下:

            gridStations.AutoGenerateColumns = false;
            stationEntityBindingSource.DataSource = _stations;
            gridStations.DataSource = stationEntityBindingSource;

请注意,_stations 是一个List<StationEntity>. 如果我直接绑定 _stations,网格将不会引发删除事件,但在我使用 BindingSource 时会引发。在我的删除中,我对集合重新编号:

        private void gridStations_BeforeDeleteRow(object sender, C1.Win.C1FlexGrid.RowColEventArgs e)
    {
        if (e.Row < 0 || e.Row > _stations.Count)
        {
            e.Cancel = true;
            return;
        }

        var row = gridStations.Rows[e.Row];
        if (row.DataSource != null)
        {
            var _toDelete = row.DataSource as StationEntity;
        }
    }

    private void gridStations_AfterDeleteRow(object sender, C1.Win.C1FlexGrid.RowColEventArgs e)
    {
        if (_toDelete.IsNotNull())
        {
            _stations.Remove(_toDelete);

            var station = 1;

            foreach (var item in _stations)
            {
                item.ID = station++;
            }

            stationEntityBindingSource.DataSource = null;
            gridStations.Update();
            stationEntityBindingSource.DataSource = _stations;
            gridStations.Update();

            _toDelete = null;
        }
    }

即使清除 BindingSource 的 DataSource 并重新应用它,网格仍然不显示更新的值。我究竟做错了什么?

4

1 回答 1

1
if (_toDelete.IsNotNull())
        {
            _stations.Remove(_toDelete);

            var station = 1;

            foreach (var item in _stations)
            {
                item.ID = station++;
            }

            stationEntityBindingSource.DataSource = null;
            gridStations.Update();
            stationEntityBindingSource.DataSource = _stations;

            //add this line
            gridStations.DataSource = null;
            gridStations.DataSource = stationEntityBindingSource;

            _toDelete = null;
        }
于 2014-05-06T13:07:08.873 回答