Click here to Skip to main content
Click here to Skip to main content

Data Pivot

, 4 Apr 2013
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

GJI

United States United States
No Biography provided

Comments and Discussions

 
QuestionPlease help PinmemberMember 1021427616-Aug-13 10:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.8.140721.1 | Last Updated 4 Apr 2013
Article Copyright 2013 by GJI
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid