New to Telerik UI for WPF? Download free 30-day trial

Using LocalDataSourceProvider

LocalDataSourceProvider is one of the data source providers that can be used with RadPivotGrid and RadPivotFieldList. LocalDataSourceProvider provides data access to a local source such as an IList of instances of user defined classes. In fact, any collection that implements the IEnumerable interface can be used as a LocalDataSourceProvider.

Defining LocalDataSourceProvider

The LocalDataSourceProvider class is part of Telerik.Pivot.Core assembly, namespace: "Telerik.Pivot.DataProviders".

You can define the LocalDataSourceProvider as a StaticResource in your XAML if it will be used in more than one control (for example, if you have RadPivotGrid and RadPivotFieldList controls in your application) or define it directly for any of the controls you are planning to use:

The pivot namespace is the URI namespace: xmlns:pivot="http://schemas.telerik.com/2008/xaml/presentation/pivot". It is mandatory to define it if you are using the LocalDataSourceProvider in your XAML.

  • Defined as a StaticResource and used in RadPivotGrid and RadPivotFieldList:

<Grid> 
    <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="*"/> 
        <ColumnDefinition Width="250"/> 
    </Grid.ColumnDefinitions> 
    <Grid.Resources> 
        <pivot:LocalDataSourceProvider x:Key="LocalDataSourceProvider"> 
        </pivot:LocalDataSourceProvider> 
    </Grid.Resources> 
 
    <pivot:RadPivotGrid Name="radPivotGrid1" DataProvider="{StaticResource LocalDataProvider}"> 
    <pivot:RadPivotFieldList Name="radPivotFieldList1" Grid.Column="1"  DataProvider="{StaticResource LocalDataProvider}"/> 
</Grid> 

If you set different DataProviders for RadPivotGrid and RadPivotFieldList you will not be able to see any changes in RadPivotGrid, even when you change something in RadPivotFieldList.

You can also create an object of type LocalDataSourceProvider in the background and after that, you can use it for your controls:

LocalDataSourceProvider localDataProvider = new LocalDataSourceProvider(); 
this.radPivotGrid1.DataProvider = localDataProvider; 
this.radPivotFieldList1.DataProvider = localDataProvider; 
Dim localDataProvider As New LocalDataSourceProvider() 
Me.radPivotGrid1.DataProvider = localDataProvider 
Me.radPivotFieldList1.DataProvider = localDataProvider 

Adding Data to LocalDataSourceProvider

The LocalDataSourceProvider has an ItemsSource and it is mandatory to set it if you want to display any data. ItemsSource is a dependency property and you are able to use binding for it, but we recommend setting it in code:

LocalDataSourceProvider localDataProvider = new LocalDataSourceProvider(); 
localDataProvider.ItemsSource = MyCollection; 
this.radPivotGrid1.DataProvider = localDataProvider; 
this.radPivotFieldList1.DataProvider = localDataProvider; 
Dim localDataProvider As New LocalDataSourceProvider() 
localDataProvider.ItemsSource = MyCollection 
Me.radPivotGrid1.DataProvider = localDataProvider 
Me.radPivotFieldList1.DataProvider = localDataProvider 

The MyCollection can be any collection that implements the IEnumerable interface or a datatable.

Adding Group Descriptions Collections

When initializing the LocalDataSourceProvider in the code-behind, it is a good idea to wrap all modifications in a BeginInit() - EndInit() section. This will cause only one refresh of the DataProvider and it will be when the EndInit() is reached. If you are applying only modifications (more than one) on an already initialized LocalDataSourceProvider, you should use the DeferRefresh() method, which will cause delay of the refresh. This way, all of your changes will be applied simultaneously. You can find more information for these methods in the Populating with Data - Overview article.

The LocalDataSourceProvider is using four different collections for the data that it holds:

  • RowGroupDescription - The data added to this description will be shown as row headers in RadPivotGrid and RadPivotFieldList. The properties can be defined as PropertyGroupDescription, DateTimeGroupDescription, DoubleGroupDescription or you can create custom implementation of PropertyGroupDescriptionBase class. Here's how to define the RowGroupDescriptions in your application:

<pivot:LocalDataSourceProvider.RowGroupDescriptions> 
    <pivot:PropertyGroupDescription PropertyName="Name" /> 
    <pivot:DateTimeGroupDescription PropertyName="Date" Step="Month" /> 
</pivot:LocalDataSourceProvider.RowGroupDescriptions> 

Telerik.Pivot.Core.PropertyGroupDescription propertyGroupDescription = new Telerik.Pivot.Core.PropertyGroupDescription(); 
propertyGroupDescription.PropertyName = "Name"; 
DateTimeGroupDescription dateTimeGroupDescription = new DateTimeGroupDescription(); 
dateTimeGroupDescription.PropertyName = "Date"; 
dateTimeGroupDescription.Step = DateTimeStep.Month; 
 
using (localDataProvider.DeferRefresh()) 
{ 
    localDataProvider.RowGroupDescriptions.Add(propertyGroupDescription); 
    localDataProvider.RowGroupDescriptions.Add(dateTimeGroupDescription); 
}; 
Dim propertyGroupDescription As New Telerik.Pivot.Core.PropertyGroupDescription() 
propertyGroupDescription.PropertyName = "Name" 
Dim dateTimeGroupDescription As New DateTimeGroupDescription() 
dateTimeGroupDescription.PropertyName = "Date" 
dateTimeGroupDescription.Step = DateTimeStep.Month 
Using localDataProvider.DeferRefresh() 
    localDataProvider.RowGroupDescriptions.Add(propertyGroupDescription) 
    localDataProvider.RowGroupDescriptions.Add(dateTimeGroupDescription) 
End Using 
  • ColumnGroupDescription - The data added to this description will be shown as column headers in RadPivotGrid and RadPivotFieldList. The properties can be defined as PropertyGroupDescription, DateTimeGroupDescription, DoubleGroupDescription or you can create a custom implementation of the PropertyGroupDescriptionBase class. Here's how to define the ColumnGroupDescriptions in your application:

<pivot:LocalDataSourceProvider.ColumnGroupDescriptions> 
    <pivot:DoubleGroupDescription PropertyName="Price"/> 
</pivot:LocalDataSourceProvider.ColumnGroupDescriptions> 

DoubleGroupDescription doubleGroupDescription = new DoubleGroupDescription(); 
doubleGroupDescription.PropertyName = "Price"; 
localDataProvider.ColumnGroupDescriptions.Add(doubleGroupDescription); 
Dim doubleGroupDescription As New DoubleGroupDescription() 
doubleGroupDescription.PropertyName = "Price" 
localDataProvider.ColumnGroupDescriptions.Add(doubleGroupDescription) 
  • AggregateDescriptions - The data added to this description will be aggregated and included in a RadPivotGrid as cells. The properties can be defined as PropertyAggregateDescription or you can create a custom implementation of the PropertyAggregateDescriptionBase class.

<pivot:LocalDataSourceProvider.AggregateDescriptions> 
    <pivot:PropertyAggregateDescription PropertyName="Price" StringFormat="C" AggregateFunction="Average"/> 
    <pivot:PropertyAggregateDescription PropertyName="Quantity"/> 
</pivot:LocalDataSourceProvider.AggregateDescriptions> 

PropertyAggregateDescription propertyAggregateDescription1 = new PropertyAggregateDescription(); 
propertyAggregateDescription1.PropertyName = "Price"; 
propertyAggregateDescription1.StringFormat = "C"; 
propertyAggregateDescription1.AggregateFunction = AggregateFunctions.Average; 
 
PropertyAggregateDescription propertyAggregateDescription2 = new PropertyAggregateDescription(); 
propertyAggregateDescription2.PropertyName = "Quantity"; 
 
using (localDataProvider.DeferRefresh()) 
{ 
    localDataProvider.AggregateDescriptions.Add(propertyAggregateDescription1); 
    localDataProvider.AggregateDescriptions.Add(propertyAggregateDescription2); 
}; 
Dim propertyAggregateDescription1 As New PropertyAggregateDescription() 
propertyAggregateDescription1.PropertyName = "Price" 
propertyAggregateDescription1.StringFormat = "C" 
propertyAggregateDescription1.AggregateFunction = AggregateFunctions.Average 
 
Dim propertyAggregateDescription2 As New PropertyAggregateDescription() 
propertyAggregateDescription2.PropertyName = "Quantity" 
 
Using localDataProvider.DeferRefresh() 
    localDataProvider.AggregateDescriptions.Add(propertyAggregateDescription1) 
    localDataProvider.AggregateDescriptions.Add(propertyAggregateDescription2) 
End Using    

With the R2 2016 release of UI for WPF, a brand new property, IgnoreNullValues, was introduced for the PropertyAggregateDescription. This property is of type bool and it is used to determine whether a specific PropertyAggregateDescription should ignore the null values when calculating its result. The default value of the property is false, so in order to ignore the null values, you should set the property to true.

Here's how to define the AggregateDescriptions in your application with a set IgnoreNullValues property:

<pivot:LocalDataSourceProvider.AggregateDescriptions> 
    <pivot:PropertyAggregateDescription PropertyName="Price" StringFormat="C" AggregateFunction="Average" IgnoreNullValues="true"/> 
    <pivot:PropertyAggregateDescription PropertyName="Quantity"/> 
</pivot:LocalDataSourceProvider.AggregateDescriptions> 

In order to set the IgnoreNullValues to true for all PropertyAggregateDescriptions that you want to add in the LocalDataSourceProvider, you should handle the LocalDataSourceProvider.PrepareDescriptionForField event and set IgnoreNullValues in the handler:

private void LocalDataSourceProvider_PrepareDescriptionForField(object sender, PrepareDescriptionForFieldEventArgs e) 
{ 
    var description = e.Description as PropertyAggregateDescription; 
    if (description != null) 
    { 
        description.IgnoreNullValues = true; 
    } 
} 
Private Sub LocalDataSourceProvider_PrepareDescriptionForField(sender As Object, e As PrepareDescriptionForFieldEventArgs) 
    Dim description = TryCast(e.Description, PropertyAggregateDescription) 
    If description IsNot Nothing Then 
        description.IgnoreNullValues = True 
    End If 
End Sub 
  • FilterDescriptions - The data added to this description will be filtered and after that, included in RadPivotGrid. The properties can be defined as PropertyFilterDescription or you can create a custom implementation of the PropertyFilterDescriptionBase class.

<pivot:LocalDataSourceProvider.FilterDescriptions> 
    <pivot:PropertyFilterDescription PropertyName="Name"> 
        <pivot:PropertyFilterDescription.Condition> 
            <pivot:TextCondition Comparison="BeginsWith" Pattern="N" /> 
        </pivot:PropertyFilterDescription.Condition> 
    </pivot:PropertyFilterDescription> 
</pivot:LocalDataSourceProvider.FilterDescriptions> 

TextCondition txtCondition = new TextCondition(); 
txtCondition.Comparison = TextComparison.BeginsWith; 
txtCondition.Pattern = "N"; 
 
PropertyFilterDescription filterDescription = new PropertyFilterDescription(); 
filterDescription.PropertyName = "Name"; 
filterDescription.Condition = txtCondition; 
localDataProvider.FilterDescriptions.Add(filterDescription); 
Dim txtCondition As New TextCondition() 
txtCondition.Comparison = TextComparison.BeginsWith 
txtCondition.Pattern = "N" 
 
Dim filterDescription As New PropertyFilterDescription() 
filterDescription.PropertyName = "Name" 
filterDescription.Condition = txtCondition 
localDataProvider.FilterDescriptions.Add(filterDescription) 

Adding Property Descriptions

All property description classes will inherit the DescriptionBase abstract class. That's why all of them have the following properties:

  • PropertyName - This is the most important property. It must be set to the property of the data that will be represented with this property description.

  • CustomName - Sets the name that will be shown instead of the property name in RadPivotGrid and RadPivotFieldList.

Here is a list of the property descriptions that you can use:

  • PropertyGroupDescription: Available for RowGroupDescriptions and ColumnGroupDescriptions.

  • DoubleGroupDescription: Available for RowGroupDescriptions and ColumnGroupDescriptions. Used when the data is of type Double. One of the imporant properties is Step, which is used to define the size of the generated groups.

  • DateTimeGroupDescription: Available for RowGroupDescriptions and ColumnGroupDescriptions. Used when the data is of type DateTime. The Step property is very useful; you can set it if the grouping should be on day, month or year.

  • PropertyFilterDescription: Available for FilterDescriptions only. The important property here is Condition as the filtering is done based on it. You can use four conditions: ComparisonCondition, IntervalCondition, SetCondition or TextCondition.

  • PropertyAggregateDescription: Available for AggregateDescriptions only. You have to define the AggregateFunction that will be used. You can use various predefined functions including Average, Sum, Min, Max, etc.

See Also

In this article