I have a DataViewGrid which has its DataSource property set to a collection of objects. This works well and displays the values of the object's properties nicely in the grid. However, when I add a new object to the collection and rerun the code to assign the collection to the grid, the grid does not update with the newly added row, rather it still shows the old un-updated collection.
After a bit of experimentation, I found I had to first null the grid's data source for it to show the updated collection.
private void uxAddSnameHistBtn_Click(object sender, EventArgs e)
{
...
clsSurnameHistory snh = new clsSurnameHistory();
snh.DateChanged = DateTime.Now.Date;
snh.NewSname = "New Surname";
...
_pensioner.SurnameHistory.Add( snh );
...
PopulateSurnameHistory();
}
private void PopulateSurnameHistory()
{
...
uxSnameHistGrd.DataSource = null;
uxSnameHistGrd.DataSource = _pensioner.SurnameHistory;
...
}
The above code is executed as part of a function which populates the grid with the object collection when the form is first loaded and when the
SurnameHistory
collection has a new object added.
Why is it necessary to null the datasource before assigning the updated collection to the grid? I was under the impression binding the collection to the grid would automatically update the grid with the new object when the collection has the new object added to it. But this is certainly not the displayed behaviour.