Showing posts with label grid. Show all posts
Showing posts with label grid. Show all posts

How to know when the sort is changed on the Xceed WPF grid

There is no easy to use sort event on the on the Xceed WPF grid itself however there is a not so obvious way that you can detect when the sort is changed by wiring up the following code.

DataGridCollectionView view = grid.ItemsSource as DataGridCollectionView;
      if (view != null)
      {
            ((INotifyCollectionChanged)view.SortDescriptions).CollectionChanged -=
                                                      new NotifyCollectionChangedEventHandler(SortCollectionChanged);
}

      private void SortCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
      {
            //do stuff
      }

WPF Xceed Grid: How to prevent/disable validation for objects that implement IDataErrorInfo


From the Xceed documentation:

"Built-in support for IDataErrorInfo provides business-object level validation that can be used in combination with validation rules.

Unless the ValidationRules collection is cleared, it will always contain an ExceptionValidationRule and DataErrorValidationRule. If the DataErrorValidationRule is excluded from the collection of validation rules, validation errors reported by IDataErrorInfo will be ignored. "

The solution is to manually loop over the columns and clear the validators

    foreach(Column c in grid.Columns)
    {
                c.CellValidationRules.Clear();
    }

How to iterate over the rows and cells in an Xceed WPF DataGrid

I found this handy code on the Xceed forums today. See http://xceed.com/CS/forums/permalink/26496/26518/ShowThread.aspx#26518

            foreach (object item in thisDataGrid1.Items)
            {
                Dispatcher.BeginInvoke(new Action<object>(DoRow), DispatcherPriority.ApplicationIdle, item);
            }

        private void DoRow(object item)
        {
            Xceed.Wpf.DataGrid.DataRow row = this.DataGrid1.GetContainerFromItem(item) as Xceed.Wpf.DataGrid.DataRow;
            if (row != null)
            {
                foreach (Xceed.Wpf.DataGrid.DataCell c in row.Cells)
                {
                    if (c != null)
                    {
                        //Do something to the cell
                    }
                }
            }
        }