Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Data Pivot

0.00/5 (No votes)
4 Apr 2013 1  
Small class to easily pivot data in a simple data table.

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here