如何设置gridview以显示多年的数据? [英] How to setup gridview to show data in years?

查看:71
本文介绍了如何设置gridview以显示多年的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Gridview,可以显示我的数据,但我想更改Gridview的视图。我拥有的是我的Gridview:



ID |名称|年|注册|

1001 Mike 2013 1500

1001 Mike 2014 1700

1001 Mike 2015 2500



我想要做的是让数据显示如下:



ID |名称| 2013年| 2014年| 2015年|

1001迈克1500 1700 2500



这样做的最佳方式是什么?



我尝试了什么:



我尝试过Listview,Datalist和Detailsview。

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.

推荐答案

您不应该使用GridView,而是使用数据!

您在此处描述的转换称为PIVOTing ...与数据库连接查找你正在使用...
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...


根据具体情况,有两种方法可以实现:

1.在SQL服务器端 - 使用 PIVOT [ ^ ]

Depending on situation, there are two ways to achieve that:
1. On SQL server side - using PIVOT[^]
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



如何使用它?请参阅:如何:创建并执行返回行的SQL语句 [ ^ ]



2.在应用程序级别 - 使用Linq查询

简化数据表简化 [ ^ ]

C#LINQ Pivot()函数 - 反思IT [ ^ ]

对数据透视表使用lambda或LINQ [ ^ ]


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[^]


这篇关于如何设置gridview以显示多年的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆