Why does the DataGrid not update when the ItemsSource is changed?

85,110

Solution 1

The ItemsSource is always the same, a reference to your collection, no change, no update. You could null it out before:

dgOrderDetail.ItemsSource = null;
dgOrderDetail.ItemsSource = OrderDetailObjects;

Alternatively you could also just refresh the Items:

dgOrderDetail.ItemsSource = OrderDetailObjects; //Preferably do this somewhere else, not in the add method.
dgOrderDetail.Items.Refresh();

I do not think you actually want to call UpdateLayout there...

(Refusing to use an ObservableCollection is not quite a good idea)

Solution 2

I also found that just doing

dgOrderDetails.Items.Refresh();

would also accomplish the same behavior.

Solution 3

If you bind the ItemSource to a filtered list with for example Lambda its not updated. Use ICollectionView to solve this problem (Comment dont work):

//WindowMain.tvTemplateSolutions.ItemsSource = this.Context.Solutions.Local.Where(obj=>obj.IsTemplate); // templates
ICollectionView viewTemplateSolution = CollectionViewSource.GetDefaultView(this.Context.Solutions.Local);
viewTemplateSolution.SortDescriptions.Clear();
viewTemplateSolution.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
viewTemplateSolution.Filter = obj =>
{
   Solution solution = (Solution) obj;
   return solution.IsTemplate;
};
WindowMain.tvTemplateSolutions.ItemsSource = viewTemplateSolution;

Solution 4

i use ObservableCollection as my items collection and than in the view model call CollectionViewSource.GetDefaultView(my_collection).Refresh();

Share:
85,110
Pouyan
Author by

Pouyan

I'm a Software Engineer and Microsoft Technology Specialist with more than 8 years of experience in developing Web and Windows Applications using C#, ASP.Net, WPF, SharePoint, etc.

Updated on July 09, 2022

Comments

  • Pouyan
    Pouyan almost 2 years

    I have a datagrid in my wpf application and I have a simple problem. I have a generic list and I want to bind this collection to my datagrid data source every time an object is being added to the collection. and I'm not interested to use observable collection.

    the point is I'm using the same method somewhere else and that works fine. but this time when i press Add button an object is added and datagrid updates correctly but from the second item added to collection datagrid does not update anymore.

    Here is the Code :

     private void btnAddItem_Click(object sender, RoutedEventArgs e)
        {
            OrderDetailObjects.Add(new OrderDetailObject
            {
                Price = currentitem.Price.Value,
                Quantity = int.Parse(txtQuantity.Text),
                Title = currentitem.DisplayName,
                TotalPrice = currentitem.Price.Value * int.Parse(txtQuantity.Text)
            });
    
            dgOrderDetail.ItemsSource = OrderDetailObjects;
            dgOrderDetail.UpdateLayout();
        }
    

    any idea ?

  • RoelF
    RoelF over 9 years
    This is a solution for this case, but it's just a band-aid. The real solution would be to use DataBinding and ObservableCollection. There are plenty of examples of that around.