Hello,
I am trying to use a simple DataTable or an XML file (generated by another application) as a FlatDataSource for the xamPivotGrid. This should be easy I assume but I do not manage to have it work.
Using the following code doesn not make it (dt is my DataTable)
FlatDataSource flatDataSource = new FlatDataSource()
{
ItemsSource = dt.AsEnumerable().ToList()
Measures = XmlaDataSource.GenerateInitialItems("AmountOfSale")*/
};
I want something very simple to plug any Table to the xamPivotGrid. I cannot define a priori a class to serialize the dataTable (or XML file) so it is not so obivious to create the IEnumerable. Thanks for your help.
To bind a table as a data cource for pivot grid you should do a little extra work. With the help of our little framework for dynamic built of type you can easily to convert your table or xml into class with appropriated properties which can be read from pivotGrid.
The below is a sample how to do that.
this.table - is the instance of your data table
using Infragistics.Olap;
using Infragistics.Olap.FlatData;
private void BuildDataSource()
DynamicTypeBuilder typeBuilder = new DynamicTypeBuilder
DynamicAssemblyName = "MyAssembly",
DynamicTypeName = "Pane"
IList<DynamicTypePropertyInfo> properties = new List<DynamicTypePropertyInfo>();
foreach (DataColumn column in this.table.Columns)
DynamicTypePropertyInfo propertyInfo = new DynamicTypePropertyInfo
PropertyName = column.ColumnName,
PropertyType = column.DataType
properties.Add(propertyInfo);
}
Type dynamicType = typeBuilder.GenerateType(properties);
Type listType = typeof(List<>);
Type genericListType = listType.MakeGenericType(dynamicType);
IList list = (IList)Activator.CreateInstance(genericListType);
foreach (DataRow dataRow in this.table.Rows)
object myDynamicInstance = Activator.CreateInstance(dynamicType);
PropertyInfo propertyVal = dynamicType.GetProperty(column.ColumnName);
if (dataRow[column] != DBNull.Value)
propertyVal.SetValue(myDynamicInstance, dataRow[column], null);
list.Add(myDynamicInstance);
ItemsSource = list,
Cube = DataSourceBase.GenerateInitialCube("Pane"),
// if you know the names of demensions you want to be in rows and columns
// you can define them here
Columns = DataSourceBase.GenerateInitialItems("[Columns]"),
Rows = DataSourceBase.GenerateInitialItems("[Row]"),
Measures = DataSourceBase.GenerateInitialItems("Value")
pivotDataSelector.DataSource = flatDataSource;
pivotGrid.DataSource = flatDataSource;
You can read more about dinamic type creation here.
Hello, I have the Infragistics 2014.2 version and I am trying to use this framework to bind a XML file to the flatdatasource.
I referenced all Infragistics dll, but my solution cannot find a reference to DynamicTypeBuilder. Again, I am using Winforms and the 2014.2 version.
Thanks for your help.
Hi,
I am using XamPivotGrid. I want to edit a rows/columns in the PivotGrid. Instead of doing it via the grid, I want to modify the data source.
I use FlatDataSource as above. How ca I retrieve and edit FlatDataSource.itemSource dynamically.
Thanks
I translated the C# Source to VB .Net.
But i still have a problem. When i Assign the FlatDataSource to a XamPivotGridSelector the List of Measures is always empty.
Is there something wrong with my code ?
Christian Gill
---------
Private Sub BuildDataSourceFromDataTable(ByVal InputTable As System.Data.DataTable, ByVal TabellenName As String, ByVal MeasureList As List(Of String), ByRef OutputFlatDataSource As FlatData.FlatDataSource) Dim Column As System.Data.DataColumn Dim PropertyInfo As DynamicTypePropertyInfo Dim Row As System.Data.DataRow Dim MeasureString As String Dim typeBuilder As New DynamicTypeBuilder With typeBuilder .DynamicAssemblyName = "MyAssembly" .DynamicTypeName = TabellenName End With Dim Properties As IList(Of DynamicTypePropertyInfo) = New List(Of DynamicTypePropertyInfo) For Each Column In InputTable.Columns PropertyInfo = New DynamicTypePropertyInfo With PropertyInfo .PropertyName = Column.ColumnName .PropertyType = Column.DataType End With Properties.Add(PropertyInfo) Next Dim DynamicType As System.Type = typeBuilder.GenerateType(Properties) Dim ListType = GetType(List(Of )) Dim GenericListType = ListType.MakeGenericType(DynamicType) Dim List As IList = Activator.CreateInstance(GenericListType) Dim MeasureStringList As String = "" For Each Row In InputTable.Rows Dim MyDynamicInstance As Object = Activator.CreateInstance(DynamicType) For Each Column In InputTable.Columns Dim PropertyVal As System.Reflection.PropertyInfo = DynamicType.GetProperty(Column.ColumnName) If IsDBNull(Row(Column)) = False Then PropertyVal.SetValue(MyDynamicInstance, Row(Column), Nothing) End If Next List.Add(MyDynamicInstance) Next OutputFlatDataSource = New FlatData.FlatDataSource With OutputFlatDataSource .ItemsSource = List .Cube = DataSourceBase.GenerateInitialCube(TabellenName) .Columns = DataSourceBase.GenerateInitialItems("[Columns]") .Rows = DataSourceBase.GenerateInitialItems("[Row]") For Each MeasureString In MeasureList If MeasureStringList.Length > 0 Then MeasureStringList = MeasureStringList + "," End If MeasureStringList = MeasureStringList + MeasureString Next .Measures = DataSourceBase.GenerateInitialItems(MeasureStringList) End With For Each Column In InputTable.Columns Dim Hier As New FlatData.HierarchyDescriptor Hier.SourcePropertyName = Column.ColumnName Dim AllLevel As New FlatData.HierarchyLevelDescriptor AllLevel.LevelName = "Total " + Column.ColumnName Dim EntriesLevel As New FlatData.HierarchyLevelDescriptor EntriesLevel.LevelName = Column.ColumnName EntriesLevel.LevelExpressionPath = "" Hier.LevelDescriptors.Add(AllLevel) Hier.LevelDescriptors.Add(EntriesLevel) OutputFlatDataSource.HierarchyDescriptors.Add(Hier) Next End Sub
I am impressed. Thanks a lot. I will give it a try.