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.
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>();
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]);
}
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);
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.
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); }
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:
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.
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?