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
710
Using a simple XML file or a DataTable as FlatDataSource
posted

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.

Parents
No Data
Reply
  • 7922
    posted

    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);

            foreach (DataColumn column in this.table.Columns)

            {

                PropertyInfo propertyVal = dynamicType.GetProperty(column.ColumnName);

                if (dataRow[column] != DBNull.Value)

                {

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

                }

            }

           

            list.Add(myDynamicInstance);

        }

     

     

        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("[Columns]"),

            Rows = DataSourceBase.GenerateInitialItems("[Row]"),

            Measures = DataSourceBase.GenerateInitialItems("Value")

        };

     

     

        pivotDataSelector.DataSource = flatDataSource;

        pivotGrid.DataSource = flatDataSource;

    }

    You can read more about dinamic type creation here.

     

Children