Click here to Skip to main content
11,929,675 members (59,321 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked

Data Pivot

, 4 Apr 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Small class to easily pivot data in a simple data table.


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)


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;

Points of Interest

One happy director, now has more numbers to enjoy.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionPlease help Pin
Member 1021427616-Aug-13 11:18
memberMember 1021427616-Aug-13 11:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151126.1 | Last Updated 4 Apr 2013
Article Copyright 2013 by GJI
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid