Automatically exported from code.google.com/p/pivot-tools Last Update: 1/22/2010
Pivot-tools is C# code designed to pivot data and incorporate that pivoted data into WebForms
controls, like GridViews
.
It is designed to be modifiable and be easily incorporated into existing code.
Pivot-tools is adapted from work done by Jeff Smith here.
It is a work in progress - feel free to open issues for any questions you may have.
Say you have the following DataTable
:
Product | Region | Sales |
---|---|---|
Cheese | U.S. | 1,000 |
Eggs | U.S. | 1,100 |
Eggs | Canada | 1,000 |
And you want to transform it into this:
Product | U.S. | Canada |
---|---|---|
Cheese | 1,000 | 0 |
Eggs | 1,100 | 1,000 |
Call PivotTools.Pivot(iDataReader, "Product", "Region", "Sales")
; this will return a PivotedTable
, which is just a subclass of DataTable
. The table will look like the bottom example.
If you have autogenerated columns, the new pivot columns will automatically be added. Just use the PivotedTable
as your data source.
If you don't have autogenerated columns, you can still add the pivoted columns into your web control. Just call PivotedTable.AddPivotColumnsToGridView(GridView gv)
.
There are alternatives for letting you do your pivots in SQL, for example, see here and here. However, SQL pivoting is not well suited for the task, as it requires dynamic queries, and is not as quick. C# pivoting is more flexible, and faster (See the following).
Open an issue if you're looking for something. Multiple pivot column support - something we might want to add in the future.