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?
/// <summary>
/// ig requires data tables be converted to dynamic classes for use as datasources..
///
/// http://blogs.infragistics.com/forums/p/46138/248517.aspx
/// DynamicAssemblyName = typeof(IgUtil).Assembly.GetName().Name
/// DynamicTypeName = dt.TableName
/// </summary>
public static IList ToDynamicList(this DataTable dt)
{
if (dt.TableName == null)
throw new ArgumentNullException("TableName cannot be null.");
}
var typeBuilder = new DynamicTypeBuilder
DynamicAssemblyName = typeof(IgUtil).Assembly.GetName().Name,
DynamicTypeName = dt.TableName
};
var properties = new List<DynamicTypePropertyInfo>();
foreach (DataColumn column in dt.Columns)
var propertyInfo = new DynamicTypePropertyInfo
PropertyName = column.ColumnName,
PropertyType = column.AllowDBNull ? column.DataType.ToNullable() : column.DataType
properties.Add(propertyInfo);
var dynamicType = typeBuilder.GenerateType(properties);
var listType = typeof(List<>);
var genericListType = listType.MakeGenericType(dynamicType);
var list = (IList)Activator.CreateInstance(genericListType);
foreach (DataRow dataRow in dt.Rows)
object myDynamicInstance = Activator.CreateInstance(dynamicType);
var propertyVal = dynamicType.GetProperty(column.ColumnName);
if (dataRow[column] != DBNull.Value)
propertyVal.SetValue(myDynamicInstance, dataRow[column], null);
list.Add(myDynamicInstance);
return list;
DLVendor, I am running into the same issue with the formatting not working. Could you post the final working version of your code, including the ToDynamicList function?
ToDynamicList
Hello DLVendor,
I am really glad that you manage to resolve your issue. If you still have any additional questions on this matter please do not hesitate to ask.
It seems like I had to set FlatDataSource.DimensionsGenerationMode