Click here to Skip to main content
15,912,069 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
        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()
        {
...
                //Load grid with history. Null it 1st in case we're updating it with the Add button.
                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.
Posted

You can go for Obervable Collection as item source,because it will update the collection when any add or remove operation occures.

http://msdn.microsoft.com/en-us/library/ms748365.aspx[^]
 
Share this answer
 
Comments
wkiess01 1-Mar-13 0:25am    
I tried the ObservableCollection as suggested by changing the field type to ObservableCollection as well as any other areas where the field was used. I then removed the setting to null and the (re)populating call. Unfortunately this did not update the grid and failed to operate just as though I was using a normal Collection. I had to again set the DataSource to null and (re)PopulateSurnameHistory for it to show up in the grid.
josh-jw 1-Mar-13 2:59am    
Check that observable collection contain updated value.
wkiess01 4-Mar-13 22:08pm    
I checked and the Observable Collection has a new item in it, but it still won't show in the grid. I even tried a grid.Refresh, but this didn't work either.
I just found this web page which explains why the ObservableCollection won't work with the DataGridView control:

http://stackoverflow.com/questions/1723041/a-good-collection-to-use-when-binding-to-a-datagridview-in-c-sharp[^]

I guess I'll just have to null the DataGridView control and bind it again...Just thought there was a more elegant solution...
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900