Introduction
This is a simple C# class to pivot a single data field.
I often need to take a data list (Product x Category x Date = Value) and show it in a simple pivoted list. After many attempts using
lists and arrays I tried using DataTable / Row etc. This code has now been in use with significant data and
has proved itself quick and reliable. It also contains a bit of what I have learnt about data tables so will also be
useful for this.
The big advantage of this code for me was its ability to handle missing data without screwing up the results (e.g., months with no data)
Background
All the usage so far has been in retail and logistics processing, deriving simple Select Product, Category, Colour, Date, Stock from [Stock History]
type of data. Pivoting
value (stock) by date.
Using the code
Since the code is a static class there is no need to instantiate it. So as long as the class is 'visible'..
DataTable dtP = SharedFunctions.Common.CPivot.Pivot(dt, "Stock", "Date", "MMM yy");
gvResults.DataSource = dtP;
gvResults.DataBind();
Points of Interest
One happy director, now has more numbers to enjoy.