I am trying to use UltraPivotGrid dynamically from DataTables. I need to create FlatDataSources from these DataTables and use them in PivotGrid. The FlatDataSource is created with data after converting DataTable to list. But when I set the FlatDataSource to PivotGrid, it does not work. Here is the code:
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 } }
Hello Alex,
1. Formatting can be achieved with a style that targets PivotCellControl. We also have a topic which uses the CellControlAttached event to customize the cells.
2. In WPF, each of our provided themes can be applied to the pivot grid by using the ThemeManager.
eg.
[code]
// apply selected theme ThemeManager.SetTheme(this.pivotGrid, new MetroDarkTheme());
[/code]
First you need to import the theme DLL into your projects. They are found alongside the rest of the wpf assemblies.
Let me know if you have any questions.
Thank you! found it. Now all solved except that I am not able to format or add theme to PivodGrid! I use samples but they use some igFramework:SampleContainer that I cannot figure where to find it!
Have you tried using brackets around the property names? eg.
FlatDataSourceInitialSettings settings = new FlatDataSourceInitialSettings(); settings.Rows = "[Hierarchies].[CompanyName]"; settings.Columns = "[Hierarchies].[ProductName], [Hierarchies].[OrderDate]"; settings.Measures = "[Measures].[Quantity]";
I did it in WPF. The only problem I cannot solve is to add rows and columns programatically. I seems that the measure is added using this way but row and columns are not!
FlatDataSource flatDataSource = new FlatDataSource() {
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("Employee"), //this field is the table column name!
Rows = DataSourceBase.GenerateInitialItems("Customer"),
Measures = DataSourceBase.GenerateInitialItems("Total")
};
Something must of went wrong with the sample since the DataSelector is empty. You can download a revised sample here.
https://bit.ly/2wOo2FB
We also do not have any additional documentation, but I will raise this to our team to implement in the future.