Click here to Skip to main content
15,886,069 members
Articles / Programming Languages / C#
Tip/Trick

Data Pivot

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
4 Apr 2013CPOL 12.9K   304   6   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'..

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
GJI
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionPlease help Pin
Member 1021427616-Aug-13 10:18
Member 1021427616-Aug-13 10: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.