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 に答える