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,
Thank you for contacting Infragistics.
After investigating about the DynamicTypeBuilder class and doing some research, WinForms DynamicTypeBuilder (although it exists in WPF) has been determined to be a product idea.
You can suggest new product ideas for future versions (or vote for existing ones) at our community page for Windows Forms.
Submitting your idea will allow you to communicate directly with our product management team, track the progress of your idea at any time, see how many votes it got, read comments from other developers in the community, and see if someone from the product team has additional questions for you. Remember when submitting your idea to explain the context in which a feature would be used and why it is needed as well as anything that would prevent you from accomplishing this today. You can even add screenshots to build a stronger case. Remember that for your suggestion to be successful, you need other members of the community to vote for it. You can also link back to this thread for additional details.
Thanks and have a great day!
I posted here: https://es.infragistics.com/community/ideas/i/ultimate-ui-for-windows-forms/creating-flatdatasource-from-datatable-dynamically-and-use-it-in-winforms-pivotgrid
Hello,
This means there is no way to use DataTable in PivotGrid in WinForms? Or is there a solution to do that?
Hello Alex, I found a solution, that I attached below, but it's not something that is available out of the box. You may review our supported binding scenarios in our online documentation. WinPivotGrid Exception Sample (2).zip
Thank you for your help. I will check that. Also I was thinking to use WPF control in the WinForm with Pivot.
It's certainly possible to host WPF controls in a WinForms application. However, as complex as the PivotGrid is, I wouldn't recommend it.
Let me know if you have any questions about my sample.
I made it work with WPF. But now I am in a preliminary stage! Is there some documentation to learn more about it? It seems very interesting. The first thing is that I need to dynamically know what to go for row and columns and measure in a dynamic table! I can figure this out. But the problem is how to assign these programmatically.
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.
Let me know if you have any questions.