I have the following POCO object:
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));
}
}
I set the grid's data source as a BindingSource and set the binding source as follows:
gridStations.AutoGenerateColumns = false;
stationEntityBindingSource.DataSource = _stations;
gridStations.DataSource = stationEntityBindingSource;
Note that _stations is a List<StationEntity>
. If I bind directly against _stations, the grid will not raise delete events, but it does when I use the BindingSource. In my deletes I renumber the collection:
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;
}
}
Even after clearing the DataSource of the BindingSource and reapplying it, the grid still doesn't show the updated values. What am I doing wrong?
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;
}