Create a Pivot Table from a DataTable

后端 未结 4 1923
耶瑟儿~
耶瑟儿~ 2021-02-07 23:53

I am using C# winforms to create an application that needs to turn a datatable into a pivot table. I have the pivot table working fine from a SQL end, but creating it from a da

4条回答
  •  醉酒成梦
    2021-02-08 00:28

    Pivot table like that can be easily calculated with free NReco.PivotData aggregation library:

    DataTable t;  // assume it has: StartDateTime, Data, Tap
    var pivotData = new PivotData(
        new string[] {"StartDateTime","Tap"},
        new AverageAggregatorFactory("Data"),
        new DataTableReader(t) );
    var pvtTbl = new PivotTable(
        new [] {"StartDateTime"},  // row dimension(s)
        new [] {"Tap"}, // column dimension(s),
        pivotData);
    

    Row and column keys are represented by pvtTbl.RowKeys and pvtTbl.ColumnKeys collections; values / totals could be accessed by indexer (for example: pvtTbl[0,0].Value ) or by row+column key (for example: pivotData[new Key(new DateTime(2012, 3, 30, 11, 42, 00)), new Key(4)].Value ).

提交回复
热议问题