Hi,
Our FlatDataSource's ItemsSource is set to an ICollectionView which wraps an ObservableCollection. The items in the collection do implement INotifyPropertyChanged.
Does the grid data refresh automatically when new items are added/removed to the collection and when they fire the PropertyChanged event?
If not - what's the best way to handle this? Calling FlatDataSource.RefreshGrid whenever the collection is changed doesn't seem optimal.
We'd like to have a couple of editable columns. If RefreshGrid is called while the we're editing those columns, their value is reset before we're finished.
Thanks.
I have created a simple project that fits the scenario described above. XamPivotGrid refreshes automatically when new item is added/removed.
Thanks,
M.Yovchev
I can see it's working because you manually call the SalesCollection.Refresh() method. I was expecting the XamPivotGrid to reflect the changes upon adding items to the ObservableCollection.
BTW if you wanted to update the Sale's NumberOfUnits property and had the Pivot Grid reflect the changes - how would you do it?
I think that Refresh() method should be called when you have ICollectionView for DataSource ItemsSource or subscribe the ICollectionView items to collection changed event. Does your implementation work with other control like listbox? If so can you please share it.
If DataSource ItemsSource is an ObservableCollection then add/remove will be affected automatically in XamPivotGrid.
If you want to change NumberOfUnits property or other property you should call also RefreshGrid() method to the datadource nevermind what is the type of the DataSource ItemsSource - ICollectionView or ObservableCollection. Also changing property to object that is in ICollectionView will not refresh automatically, because this collection is not monitoring such changes.
I think I have found some way to set ICollectionView for ItemsSource and it to be updated automatically when add/remove items. It works with XamPivotGrid and other controls like listbox. May be you are missing something in your implementation of ICollectionView just like me. In this project you can search for this line of code: this.ItemsSource = CollectionViewSource.GetDefaultView(collection.Items); This is what makes the difference.
I'm updating the pivot grid when the PropertyChanged event for an item is raised by calling PivotGrid.DataSource.RefreshGrid();
The problem with this approach is that you have some cells selected you lose the selection when the grid is refreshed.
Any advise as to how to work around this?
Can you please have a look at the attached project?
I've modified your sample for the Sale class to raise a PropertyChanged event upon updating the NumberOfUnits property.
The grid is not refreshed unless I call the DataSource's RefreshGrid method. I've tried to call the ICollectionView's Refresh method but it's not working.
Hello,
I've been looking into your requirements and reviewed the attached sample application.
The reason the grid is not updated before calling the RefreshGrid() method is that the FlatDataSource does not track INotifyPropertyChanged events fired by instances of its ItemsSource. It only tracks INotifyCollectionChanged of items source and updates its UI automatically.
The selection is lost because on refreshing the grid new cell objects are created. What I can suggest in order to "preserve" the selection is subscribing for the DataSource's ResultChanged event, find the new cell(s) that should be selected and add it(them) to the SelectedCells collection off the SelectionSettings object. The code should look similar to the following snippet:
private void DataSource_ResultChanged(object sender, ResultChangedEventArgs e)
{
foreach (var cell in cells)
var colKey = cell.Column.Index;
var rowKey = cell.DataRow.HeaderText;
var row = this.pivotGrid.GridLayout.Rows.Where(r => r.HeaderText == rowKey).FirstOrDefault();
this.pivotGrid.SelectionSettings.SelectedCells.Add(row.Cells[colKey]);
}
I updated the attached sample application with the code above for your reference. Please, let me know if you need further assistance on the matter.
Hello and Happy New Year!
For the columns and rows you will also need to find the new objects based on some parameter and add them to the respective collection after the grid is refreshed. The code should look similar to the following snippet:
cells = this.pivotGrid.SelectionSettings.SelectedCells.ToList<PivotCell>();
columns = this.pivotGrid.SelectionSettings.SelectedColumns.ToList<PivotDataColumn>();
rows = this.pivotGrid.SelectionSettings.SelectedRows.ToList<PivotDataRow>();
foreach (var column in columns)
var colKey = column.Index;
var col = this.pivotGrid.GridLayout.Columns.Where(c => c.Index == colKey).FirstOrDefault();
this.pivotGrid.SelectionSettings.SelectedColumns.Add(col);
foreach (var row in rows)
var rowKey = row.HeaderText;
var currentRow = this.pivotGrid.GridLayout.Rows.Where(r => r.HeaderText == rowKey).FirstOrDefault();
this.pivotGrid.SelectionSettings.SelectedRows.Add(currentRow);
Hi Galina,
Above snippet is working fine for the SelectedCells collection.
I'm trying to achieve the same results for both SelectedColumns and SelectedRows but doesn't seem to work. Can you please advise?
foreach (var column in columns) { pivotGrid.SelectionSettings.SelectedColumns.Add(column); }
foreach (var row in rows) { pivotGrid.SelectionSettings.SelectedRows.Add(row); }