Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have a table and i pivot it on sql server but in want ton get a mvc view i using a dynamic pivot for my data cause i have some(WEEKS)
i want to pivot it on c# and display my data on mvc view.

my data source : imgt.PNG - Google Drive[^]


and my pivot result :imgt.PNG - Google Drive[^]


below my dynamic pivot.
thanks





USE [YMOReporting]
GO
/****** Object:  StoredProcedure [dbo].[test]    Script Date: 28/11/2017 13:49:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[test]

AS
BEGIN
DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX), 
		@PivotColumnsToSelect AS NVARCHAR(MAX);

SELECT @cols = STUFF((SELECT DISTINCT ',' + QUOTENAME([Week]) 
FROM  OpenOrdersWeekly_View FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'')
SET @query = 'SELECT *
            INTO MyTabTest from 
            (
                SELECT [Planner], [Vendor Location],[Bulk Material], [Value], [Week]

                FROM OpenOrdersWeekly_View
           ) x
            pivot 
            (
                 SUM([Value])
                for [Week] in (' + @cols + ')
            ) p  ORDER BY [Week];'


EXECUTE(@query)
SELECT * FROM MyTabTest
DROP TABLE MyTabTest
END


What I have tried:

i try code on below but my model return null so i can't display result on my view





using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YMOReporting.Models;
using System.Data.SqlClient;
 
namespace YMOReporting.Controllers.Reportings
{
    public class Proc1Controller : Controller
    {
        private YMOReportingConnex db = new YMOReportingConnex();
 
            // GET: Proc1 : stored for a dynamic pivot
            public ActionResult Index()
            {
                return View();
            }
        public DataTable PivotTableView()
        {
            db.GetProc1().ToList();
 
            DataTable dt = new DataTable();
 
            //GetProc1() return All data for Table.
            var data = db.GetProc1().ToList();
 
            //Applying linq for geeting pivot output
            var d = (from f in data
                     group f by new { f.Planner, f.Vendor_Location, f.Bulk_Material }
                into myGroup
                     where myGroup.Count() > 0
                     select new
                     {
                         myGroup.Key.Planner,
                         myGroup.Key.Vendor_Location,
                         myGroup.Key.Bulk_Material,
                         subject = myGroup.GroupBy(f => f.Week).Select
                             (m => new { Sub = m.Key, Value = m.Sum(c => c.Value) })
                     }).ToList();
 
            var sub = db.GetProc1().ToList();
            // Distinct Week Like Below
            //Creating array for adding dynamic columns
            ArrayList objDataColumn = new ArrayList();
 
            if (data.Count() > 0)
            {
                //Three column are fix "Planner","Vendor_Location","Bulk_Material".
                objDataColumn.Add("Planner");
                objDataColumn.Add("Vendor_Location");
                objDataColumn.Add("Bulk_Material");
 
                //Add Subject Name as column in Datatable
                for (int i = 0; i < sub.Count; i++)
                {
                    objDataColumn.Add(sub[i].Week);
                }
 
                //Add dynamic columns name to datatable dt
                for (int i = 0; i < objDataColumn.Count; i++)
                {
                    dt.Columns.Add(objDataColumn[i].ToString());
                }
 
                //Add data into datatable with respect to dynamic columns and dynamic data
                for (int i = 0; i < d.Count; i++)
                {
                    List<string> tempList = new List<string>();
                    tempList.Add(d[i].Planner.ToString());
                    tempList.Add(d[i].Vendor_Location.ToString());
                    tempList.Add(d[i].Bulk_Material.ToString());
 
                    var res = d[i].subject.ToList();
                    for (int j = 0; j < res.Count; j++)
                    {
                        tempList.Add(res[j].Value.ToString());
                    }
 
                    dt.Rows.Add(tempList.ToArray<string>());
                }
 
            }
            return dt;
 
        }
 
    }
}
Posted

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900