Version

DataSource Property

Gets/sets the source of the data to any object that implements the System.Collections.IEnumerableinterface.
Syntax
'Declaration
 
Public Property DataSource As IEnumerable
public IEnumerable DataSource {get; set;}
Exceptions
ExceptionDescription
System.InvalidOperationExceptionAttempting to set this property after items have been added directly to the DataItems collection property.
System.InvalidOperationExceptionAttempting to set this property when the IsExporting property is true.
Remarks

Any object that implements the System.Collections.IEnumerable interface (other than a string) can be set as the DataSource. The architecture can support both hierarchical and heterogenous data (multiple data types within a single list) as well as more free-form structures like trees (e.g. XML data sources). There are currently some limitations, e.g. the GroupByArea only displays the FieldLayout.Fields from the DefaultFieldLayout so users can't groupby Fields from multiple types as would be the case with heterogeneous data or data from child records.

DataRecords are created lazily to represent each item in the DataSource. The DataRecord exposes a read-only DataRecord.DataItem property that returns the associated item from the data source as well as a corresponding DataRecord.DataItemIndex property. These DataRecords are managed by the RecordManager and exposed via its RecordManager.Unsorted, RecordManager.Sorted and RecordManager.Groups collection properties.

When a DataRecord is created the FieldLayouts collection is searched for an existing FieldLayout whose FieldLayout.Fields match the DataRecord.DataItem's properties. If one is not found then a new FieldLayout is created, in which case the FieldLayoutInitializing and FieldLayoutInitialized events will be raised. If the new FieldLayout's FieldLayout.AutoGenerateFieldsResolved property returns true then the FieldLayout.Fields collection is automatically populated with a Field for every public property on the data item.

Note: This property is mutually exclusive with the DataItems property, trying to set this property and add items directly to the DataItems collection will cause an InvalidOperationException to be thrown. Also, in version 4.5 of the .NET framework Microsoft added support for cross thread updating and access of a collection via the BindingOperations' EnableCollectionSynchronization and AccessCollection methods. The DataPresenter family of controls support this as long as the DataSource property is set to a CollectionView, e.g.:

             
               var list = new ObservableCollection<MyClass>();
            
               var lockingObj = new object();
               
               // Call EnableCollectionSynchronization on the UI thread
               BindingOperations.EnableCollectionSynchronization(list, lockingObj);
               
               // Note: that XamDataGrid will only support cross-thread updating if the list is in a CollectionView
               XamDataGrid1.DataSource = CollectionViewSource.GetDefaultView(list);
               
               // perform updating of the collection on a background thread using locks
               Task.Run(new Action(() =>
               {
                   // lock using the same locking object that was passed into EnableCollectionSynchronization
                   // above on the UI thread
                   lock(lockingObj)
                   {
                       list.Add(new MyClass());
                   }
               }));
               
               // Alternatively, you can perform updating of the collection on a background thread using AccessCollection, e.g.:
               Task.Run(new Action(() =>
               {
                   // Since we will be calling AccessCollection on this thread we need to first call
                   // EnableCollectionSynchronization with the same locking object. This needs to be done
                   // on every thread that intends to call BindingOperations.AccessCollection
                   BindingOperations.EnableCollectionSynchronization(list, lockingObj);
            
                   // calling AccessCollection will wrap the action in a synchronization lock
                   BindingOperations.AccessCollection(list, new Action(() =>
                   {
                       list.Add(new MyClass());
                   }), true);
               }));
             

Requirements

Target Platforms: Windows 10, Windows 8.1, Windows 8, Windows 7, Windows Server 2012, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also