Create a Pivot Table from a DataTable using C# in Asp.net

Normal Table

CustomerName   Value
  Vinod                    25
  Sagar                     35
  Manu                     45

Pivot Table

Vinod    Sagar   Manu
   25           35        45


Public DataTable Pivot()
{
                DataTable dt = new DataTable();
                dt = "Fetch Data from Database"
                try
                {
                    DataTable dtData = new DataTable();
                    DataColumn col;
                    DataRow newRow;
                    for (int I = 0; I < dt.Columns.Count - 1; I++)
                    {
                        newRow = dtData.NewRow();
                        for (int J = 0; J < dt.Rows.Count; J++)
                        {
                         col = new DataColumn(dt.Rows[J][I].ToString(),Type.GetType("System.String"));
                            dtData.Columns.Add(col);
                        }
                    }

                    for (int I = 1; I <= dt.Columns.Count - 1; I++)
                    {
                        newRow = dtData.NewRow();
                        for (int J = 0; J < dt.Rows.Count; J++)
                        {
                            newRow[J] = dt.Rows[J][I].ToString();
                        }
                        dtData.Rows.Add(newRow);
                    }

                     return dtData;

}

Comments

  1. The Kettic PivotGrid Control support of full OLAP, Pivoting, tabular and compact layouts, filtering and sorting, group, and more functionalities.

    ReplyDelete

Post a Comment