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
595
Converting DataTable to FlatDataSource -- Custom Aggregations and Formatting
posted

Hi,

I would like to create a method that converts an arbitrary DataTable into a FlatDataSource.  

What I have now is 

        public static FlatDataSource ToDataSource(this DataTable dt)
        {
            var flatDataSource = new FlatDataSource()
                                     {
                                         //http://blogs.infragistics.com/forums/p/46138/248517.aspx
                                         ItemsSource = dt.ToDynamicList(),
                                         Cube = DataSourceBase.GenerateInitialCube("Pane"),
                                         Columns = DataSourceBase.GenerateInitialItems("[Columns]"),
                                         Rows = DataSourceBase.GenerateInitialItems("[Row]"),
                                         Measures = DataSourceBase.GenerateInitialItems("Value")
                                     };

            var cubeMetadata = new CubeMetadata();
            flatDataSource.CubesSettings.Add(cubeMetadata);

            foreach (DataColumn col in dt.Columns)
            {
                //heirarchy
                var hier = new HierarchyDescriptor
                {
                    SourcePropertyName = col.ColumnName,
                };
                var allLevel = new HierarchyLevelDescriptor
                {
                    LevelName = "All " + col.ColumnName + "s"
                };
                var entriesLevel = new HierarchyLevelDescriptor
                {
                    LevelName = col.ColumnName,
                    LevelExpressionPath = col.ColumnName
                };
                hier.LevelDescriptors.Add(allLevel);
                hier.LevelDescriptors.Add(entriesLevel);
                flatDataSource.HierarchyDescriptors.Add(hier);

                if (col.DataType == typeof(double))
                {
                    //NOTE: This doesn't work 
                    var dm = new DimensionMetadata()
                             {
                                 SourcePropertyName = col.ColumnName,
                                 DisplayName = col.ColumnName,
                                 DimensionType = DimensionType.Measure,
                                 AggregatorType = AggregatorType.Sum,
                                 DisplayFormat = "{0:N2}"
                             };
                    cubeMetadata.DimensionSettings.Add(dm);
                }

            }
                return flatDataSource;
        }
How can I get the measures to be formatted eg 0.00?
Parents Reply Children