我有以下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并重新应用它之后,网格仍然不会显示更新的值。我做错了什么?
答案 0 :(得分: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;
}