Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
Status: New
Creating FlatDataSource from DataTable dynamically and use it in WinForms PivotGrid

Hello!

In WPF there is a library called DynamicTypeBuilder  that lets us build IList or IEnumerator from a DataTable. However this does not exist in the WinForms. I used the reference to that dll of WPF to build a list from DynamicTypeBuilder  and pass it to FlatDataSource in WinForms and then set the datasource in the PivotGrid. 

The datasource is built from the list but the PivotGrid is not filled with the data in the DataSource. I think the PivotGrid must have some functionalities to use that DataSource. 

I wonder if there is any way to do this?

Here is the code. If needed I can create a program  with DataTable as well.

//For Olap I use this dll: InfragisticsWPF4.Olap.v18.1 

using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using Company.ClientLibraries;
using Infragistics.Olap.FlatData;
using System.Reflection;
using Infragistics.Olap;
using System.Collections;

DynamicTypeBuilder typeBuilder = new DynamicTypeBuilder

{
DynamicAssemblyName = "MyAssembly",
DynamicTypeName = "Pane"
};

Type dynamicType = null;


private IList GetListFromTable(DataTable table)

{
IList<DynamicTypePropertyInfo> properties = new List<DynamicTypePropertyInfo>();
foreach (DataColumn column in table.Columns)

{
DynamicTypePropertyInfo propertyInfo = new DynamicTypePropertyInfo

{
PropertyName = column.ColumnName,
PropertyType = column.DataType
};
properties.Add(propertyInfo);
}
dynamicType = typeBuilder.GenerateType(properties);
Type listType = typeof(List<>);
Type genericListType = listType.MakeGenericType(dynamicType);
IList list = (IList)Activator.CreateInstance(genericListType);
foreach (DataRow dataRow in table.Rows)
{
object myDynamicInstance = Activator.CreateInstance(dynamicType);
foreach (DataColumn column in table.Columns)

{
PropertyInfo propertyVal = dynamicType.GetProperty(column.ColumnName);
if (dataRow[column] != DBNull.Value)

{

propertyVal.SetValue(myDynamicInstance, dataRow[column], null);

}

}
list.Add(myDynamicInstance);

}
return list;
}


private void LoadPivot()
{
try
{
IList list = null;


list = GetListFromTable(this.dataTable);

if (list != null)
{

FlatDataSourceInitialSettings settings = new FlatDataSourceInitialSettings();
settings.Rows = "CustomerName";
settings.Columns = "ItemName";
settings.Measures = "Total";
this.ultraPivotGrid1.RowHeaderLayout = Infragistics.Win.UltraWinPivotGrid.RowHeaderLayout.Compact;
FlatDataSource ds = new FlatDataSource(
list, dynamicType, settings);

ds.DisplayName = "Orders by ItemName";


this.ultraPivotGrid1.SetDataSource(ds);

}
}
catch (Exception ex)
{
#if DEBUG
ErrorHelper.ProcessError(ex);
#endif
}
}

Note: I first created a thread for help on this matter, but I was told to use this page. Here is the original thread:

https://www.infragistics.com/community/forums/f/ultimate-ui-for-windows-forms/118862/creating-flatdatasource-from-datatable-dynamically-and-use-it-in-pivotgrid/528510#528510