Click here to Skip to main content
15,743,429 members
Articles / Web Development / ASP.NET
Posted 6 Dec 2009


325 bookmarked

Pivoting DataTable Simplified

Rate me:
Please Sign up or sign in to vote.
4.92/5 (131 votes)
20 Aug 2014CPOL5 min read
A class to pivot a DataTable with various aggregate functions


Displaying data in tabular form is an essential part of any application nowadays. But sometimes, you need to display a huge amount of data in terms of number of rows. It becomes very difficult to analyze if the number of rows is huge. In such cases, you may wish to summarize your data in the other formats like charts, graphs, groups, pivots, etc. This article presents a simplified way to pivot your data with an appropriate aggregate function so that you can enhance your reports easily. Based on the feedback given by various readers, more features have been provided to pivot class. The pivot class is now capable to pivot data on both the axis at a time. Moreover, it also has the facility to do sub-total column wise.

Below is a screenshot of pivoted data in a GridView:


How It Works

To simplify the scenario, I have divided the result table into three areas: RowField, DataField, and ColumnFields. If you wish to do pivot on both the axis, you may use another overload of the same method where you just need to pass RowFields parameter as an array. Apart from the area, the Pivot class provides you the option to bind your data based on some aggregate functions. The various aggregate options available are:

  • Count: Returns the count of matching data
  • Sum: Returns the sum of matching data (to get the sum, the type of the DataField must be convertible to decimal type)
  • First: Returns the first occurrence of matching data
  • Last: Returns the last occurrence of matching data
  • Average: Returns the average of matching data (to get the average, the type of the DataField must be convertible to decimal type)
  • Max: Returns the maximum value from the matching data
  • Min: Returns the minimum value from the matching data
  • Exists: Returns "true" if there is any matching data, else "false"

The code mainly contains a class named "Pivot" that takes the DataTable in the constructor. ColumnFields takes as a string array parameter which allows you to pivot data on more than one column. It contains a function called PivotData() which actually pivots your data.

public DataTable PivotData(string RowField, string DataField, 
       AggregateFunction Aggregate, params string[] ColumnFields)
    DataTable dt = new DataTable();
    string Separator = ".";
    var RowList = (from x in _SourceTable.AsEnumerable() 
        select new { Name = x.Field<object>(RowField) }).Distinct();
    var ColList = (from x in _SourceTable.AsEnumerable() 
                   select new { Name = ColumnFields.Select(n => x.Field<object>(n))
                       .Aggregate((a, b) => a += Separator + b.ToString()) })
                       .OrderBy(m => m.Name);

    foreach (var col in ColList)

    foreach (var RowName in RowList)
        DataRow row = dt.NewRow();
        row[RowField] = RowName.Name.ToString();
        foreach (var col in ColList)
            string strFilter = RowField + " = '" + RowName.Name + "'";
            string[] strColValues = 
            for (int i = 0; i < ColumnFields.Length; i++)
                strFilter += " and " + ColumnFields[i] + 
                             " = '" + strColValues[i] + "'";
            row[col.Name.ToString()] = GetData(strFilter, DataField, Aggregate);
    return dt;

PivotData method also has 2 more overloads. If you wish to show column wise sub-total, you may use the overload by passing a bool variable showSubTotal. If you wish to Pivot your data on both sides, i.e., row-wise as well as column-wise, you may wish to use another overload where you can pass rowFields and columnFields as an array.

First of all, the function determines the number of rows by getting the distinct values in RowList, and the number of columns by getting the distinct values in ColList. Then, the columns are created. It then iterates through each row and gets the matching values to the corresponding cell based on the aggregate function provided. To retrieve the matching value, the GetData() function is called.

private object GetData(string Filter, string DataField, AggregateFunction Aggregate)
        DataRow[] FilteredRows = _SourceTable.Select(Filter);
        object[] objList = 
         FilteredRows.Select(x => x.Field<object>(DataField)).ToArray();

        switch (Aggregate)
            case AggregateFunction.Average:
                return GetAverage(objList);
            case AggregateFunction.Count:
                return objList.Count();
            case AggregateFunction.Exists:
                return (objList.Count() == 0) ? "False" : "True";
            case AggregateFunction.First:
                return GetFirst(objList);
            case AggregateFunction.Last:
                return GetLast(objList);
            case AggregateFunction.Max:
                return GetMax(objList);
            case AggregateFunction.Min:
                return GetMin(objList);
            case AggregateFunction.Sum:
                return GetSum(objList);
                return null;
    catch (Exception ex)
        return "#Error";
    return null;

This function first filters out the matching RowField and ColumnFields data in the DataRow[] array and then applies the aggregate function on it.

Using the Code

Using the code is simple. Create an instance of the Pivot class and then call the PivotData method with the required parameters. The PivotData() method returns the DataTable which can directly be used as the DataSource of the GridView.

DataTable dt = ExcelLayer.GetDataTable("_Data\\DataForPivot.xls", "Sheet1$");
Pivot pvt = new Pivot(dt);

grdPivot.DataSource = pvt.PivotData("Designation", "CTC", 
   AggregateFunction.Max, "Company", "Department", "Year");

The database used as a sample is an Excel sheet and is present in the "_Data" folder of the root folder of sample application.

Merge GridView Header Cells

The MergeHeader function is created to merge the header cells to provide a simplified look.

private void MergeHeader(GridView gv, GridViewRow row, int PivotLevel)
    for (int iCount = 1; iCount <= PivotLevel; iCount++)
        GridViewRow oGridViewRow = new GridViewRow(0, 0, 
          DataControlRowType.Header, DataControlRowState.Insert);
        var Header = (row.Cells.Cast<tablecell>()
            .Select(x => GetHeaderText(x.Text, iCount, PivotLevel)))
            .GroupBy(x => x);

        foreach (var v in Header)
            TableHeaderCell cell = new TableHeaderCell();
            cell.Text = v.Key.Substring(v.Key.LastIndexOf(_Separator) + 1);
            cell.ColumnSpan = v.Count();
        gv.Controls[0].Controls.AddAt(row.RowIndex, oGridViewRow);
    row.Visible = false;

The function creates a new row for each pivot level and merges accordingly. PivotLevel here is the number of columns on which the pivot is done.

Header gets all the column values in an array, groups the repeated values returned by the GetHeaderText() function, sets the ColumnSpan property of the newly created cell according to the number of repeated HeaderText, and then adds the cell to the GridViewRow. Finally, add the GridViewRow to the GridView.

The GetHeaderText() function returns the header text based on the PivotLevel.

For example, suppose a pivot is done on three ColumnFields, namely, Company, Department, and Year. The result header of the GridView will initially have a header like Company.Department.Year for a PivotLevel 1. GetHeaderText() will return Company. For a PivotLevel 2, GetHeaderText() will return Company.Department. For a PivotLevel 3, GetHeaderText() will return Company.Department.Year, and so on...

Merge GridView Row Header Cells

This may need to be done when you are pivoting your data row-wise also. Here, we are simply merging the cells with the same text.

private void MergeRows(GridView gv, int rowPivotLevel)
        for (int rowIndex = gv.Rows.Count - 2; rowIndex >= 0; rowIndex--)
            GridViewRow row = gv.Rows[rowIndex];
            GridViewRow prevRow = gv.Rows[rowIndex + 1];
            for (int colIndex = 0; colIndex < rowPivotLevel; colIndex++)
                if (row.Cells[colIndex].Text == prevRow.Cells[colIndex].Text)
                    row.Cells[colIndex].RowSpan = (prevRow.Cells[colIndex].RowSpan < 2) ? 
                                      2 : prevRow.Cells[colIndex].RowSpan + 1;
                    prevRow.Cells[colIndex].Visible = false;

The code to merge header rows is fairly simple. It simply loops through all the row header cells from bottom to top, compares the text with previous corresponding row cell, increases the row span by 1 if same and hides the previous corresponding row.

Screen shot for both side pivot:

Both Side Pivot

Below is the screenshot of the GridView containing the third level pivoted data:


Points of Interest

Along with pivoting the DataTable, the code will also help you to merge the header cells in the desired format in GridView. Moreover, you may have a deeper look into PivotData method to know how you can search or filter data in DataTable using Linq. Apart from this, MergeRows method acts as a sample to merge rows in a GridView. For beginners, the ExcelLayer.GetDataTable() method will be a sample to get the data from the Excel Sheet.

Based on the request from many readers, I have not provided the sample to query the data from database too. You may find the SQL Script attached to create SQL Server database table and code to read data to DataTable from SQL Server.

You may also wish to consider the following link to pivot a DataTable: C# Pivot Table.

Future Consideration

Currently, the code can pivot data only for a DataTable. The code will be enhanced to pivot any object derived from an IListSource or ICollection.


  • 9th December, 2009: First version release
  • 19th March, 2010: VB.NET source and demo added


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

Written By
India India
Anurag Gandhi is a Freelance Developer and Consultant, Architect, Blogger, Speaker, and Ex Microsoft Employee. He is passionate about programming.
He is extensively involved in Asp.Net Core, MVC/Web API, Node/Express, Microsoft Azure/Cloud, web application hosting/architecture, Angular, AngularJs, design, and development. His languages of choice are C#, Node/Express, JavaScript, Asp .NET MVC, Asp, C, C++. He is familiar with many other programming languages as well. He mostly works with MS SQL Server as the preferred database and has worked with Redis, MySQL, Oracle, MS Access, etc. also.
He is active in programming communities and loves to share the knowledge with others whenever he gets the time for it.
He is also a passionate chess player.
Linked in Profile:
He can be contacted at

Comments and Discussions

QuestionCan you also explain purpose of this line? Pin
Himalay Chauhan 20212-Feb-23 15:28
Himalay Chauhan 20212-Feb-23 15:28 
QuestionCan you give suggestion on what to do if we have multiple DataFields? Pin
Himalay Chauhan 20212-Feb-23 14:24
Himalay Chauhan 20212-Feb-23 14:24 
QuestionHow To Add Total column and Row Pin
siddhesh mandavkar22-Aug-21 19:11
siddhesh mandavkar22-Aug-21 19:11 
QuestionToList() timing out Pin
Member 1482903621-Oct-20 10:08
Member 1482903621-Oct-20 10:08 
AnswerRe: ToList() timing out Pin
Anurag Gandhi15-Apr-21 6:09
professionalAnurag Gandhi15-Apr-21 6:09 
QuestionA Wonderful Work Pin
Member 1291398017-Jul-19 4:08
Member 1291398017-Jul-19 4:08 
AnswerRe: A Wonderful Work Pin
Anurag Gandhi19-Jul-19 19:04
professionalAnurag Gandhi19-Jul-19 19:04 
QuestionI have a requirement of multiple z-axis fields Pin
DeepikaSKC29-May-19 0:53
DeepikaSKC29-May-19 0:53 
AnswerRe: I have a requirement of multiple z-axis fields Pin
Anurag Gandhi19-Jul-19 19:06
professionalAnurag Gandhi19-Jul-19 19:06 
QuestionThank Pin
Member 1244545617-Jul-18 20:46
Member 1244545617-Jul-18 20:46 
AnswerRe: Thank Pin
Anurag Gandhi19-Jul-19 19:05
professionalAnurag Gandhi19-Jul-19 19:05 
QuestionDisplay Total on Columns and rows on pivot table Pin
Prayash Bhatt29-Mar-17 1:26
Prayash Bhatt29-Mar-17 1:26 
Questionupdate for EF Pin
Member 125836626-Jan-17 21:39
Member 125836626-Jan-17 21:39 
AnswerRe: update for EF Pin
Anurag Gandhi8-Jan-17 8:40
professionalAnurag Gandhi8-Jan-17 8:40 
GeneralVB.Net Rows and Columns Solution (Both Side Pivot)... Pin
bigfiveai.com23-Sep-16 7:30
bigfiveai.com23-Sep-16 7:30 
AnswerRe: VB.Net Rows and Columns Solution (Both Side Pivot)... Pin
Anurag Gandhi23-Sep-16 7:51
professionalAnurag Gandhi23-Sep-16 7:51 
GeneralRe: VB.Net Rows and Columns Solution (Both Side Pivot)... Pin
bigfiveai.com23-Sep-16 9:46
bigfiveai.com23-Sep-16 9:46 
QuestionWhat about the performance Pin
Daniel Kamisnki2-Apr-15 3:27
Daniel Kamisnki2-Apr-15 3:27 
AnswerRe: What about the performance Pin
Anurag Gandhi2-Apr-15 10:30
professionalAnurag Gandhi2-Apr-15 10:30 
QuestionHow do i show two data fields side by side for multiple dates on columns Pin
Member 114278644-Feb-15 11:52
Member 114278644-Feb-15 11:52 
AnswerRe: How do i show two data fields side by side for multiple dates on columns Pin
Anurag Gandhi5-Feb-15 3:14
professionalAnurag Gandhi5-Feb-15 3:14 
GeneralRe: How do i show two data fields side by side for multiple dates on columns Pin
Member 114278645-Feb-15 5:00
Member 114278645-Feb-15 5:00 
Questionimporting into VS 2013 incompatability issue Pin
PlayerToBeNamedLater22-Jan-14 7:35
PlayerToBeNamedLater22-Jan-14 7:35 
AnswerRe: importing into VS 2013 incompatability issue Pin
Anurag Gandhi25-Jan-14 12:33
professionalAnurag Gandhi25-Jan-14 12:33 
QuestionI get the error shown below Pin
Member 103712024-Nov-13 14:38
Member 103712024-Nov-13 14:38 

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.