Click here to Skip to main content
15,918,007 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Gridview that shows my data good but I want to change the view of the Gridview. What I have is this as my Gridview:

ID | Name | Year | Enroll |
1001 Mike 2013 1500
1001 Mike 2014 1700
1001 Mike 2015 2500

What I am trying to do is to get the data to display like this:

ID | Name | Year 2013 | Year 2014 | Year 2015 |
1001 Mike 1500 1700 2500

What is the best way of doing this?

What I have tried:

I have tried Listview, Datalist and Detailsview.
Posted
Updated 27-Feb-18 21:26pm

You should not play with the GridView, but with the data!
The transformation you are describing here call PIVOTing... Look it up in connection with database you are using...
 
Share this answer
 
Depending on situation, there are two ways to achieve that:
1. On SQL server side - using PIVOT[^]
SQL
SELECT ID, [Name], [2013], [2014], [2015]
FROM (
    SELECT  ID, [Name], [Year], Enroll
    FROM YourTable 
) AS SRC
PIVOT(MAX(Enroll) FOR [Year] IN ([2013], [2014], [2015])) AS PVT

How to use it? See: How to: Create and Execute an SQL Statement that Returns Rows[^]

2. On application level - using Linq query
Pivoting DataTable Simplified[^]
C# LINQ Pivot() Function - Reflection IT[^]
Using lambda or LINQ for pivot tables[^]
 
Share this answer
 
v2

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