日期:2014-05-18  浏览次数:20991 次

关于DataGridView数据转换的问题
第一次C#开发,帮朋友做个转换Excel数据格式的小程序,开发存在如下问题,请高手帮忙解决。
简单描述:
原始表:
序号 经度 纬度 时间
1 88 76
2 89 76
3 90 75

4 77 90
5 78 91
6 79 90
转换成目标表
序号 经度1 纬度1 经度2 纬度2 经度3 纬度3
1 88 76 89 76 90 75
4 77 90 78 91 79 90

数据量每天大概1W左右。

我首先利用OLEDB读取原始记录表,绑定DataGridView,显示出来。然后,想做个按钮,点击开始转换,形成目标表的样子利用DataGridView显示出来,最终点击“导出Excel”按钮,生成新的报表。

思路就是这样的,现在在转换这一步不知道该怎么弄,如果给目标表DataGridView绑定数据源显示。

请高手指点。

------解决方案--------------------
SQL code
--在SQL SERVER 中就是个行转列
declare @T table (
序号 int,经度 int,纬度 int)
insert into @T
select 1,88,76 union all
select 2,89,76 union all
select 3,90,75 union all
select 4,77,90 union all
select 5,78,91 union all
select 6,79,90

select 
    min(序号) as 序号,
    max(case when 序号%3=1 then 经度 else 0 end) as 经度1,
    max(case when 序号%3=1 then 纬度 else 0 end) as 纬度1,
    max(case when 序号%3=2 then 经度 else 0 end) as 经度2,
    max(case when 序号%3=2 then 纬度 else 0 end) as 纬度2,
    max(case when 序号%3=0 then 经度 else 0 end) as 经度3,
    max(case when 序号%3=0 then 纬度 else 0 end) as 纬度3
from @T group by ceiling(序号/3.0)
/*
序号          经度1         纬度1         经度2         纬度2         经度3         纬度3
----------- ----------- ----------- ----------- ----------- ----------- -----------
1           88          76          89          76          90          75
4           77          90          78          91          79          90
*/

------解决方案--------------------
转换按钮事件就是换一下数据即可。
SQL code
--转换前的数据
select * from tablename
--转换后的数据
select 
    min(序号) as 序号,
    max(case when 序号%3=1 then 经度 else 0 end) as 经度1,
    max(case when 序号%3=1 then 纬度 else 0 end) as 纬度1,
    max(case when 序号%3=2 then 经度 else 0 end) as 经度2,
    max(case when 序号%3=2 then 纬度 else 0 end) as 纬度2,
    max(case when 序号%3=0 then 经度 else 0 end) as 经度3,
    max(case when 序号%3=0 then 纬度 else 0 end) as 纬度3
from tablename group by ceiling(序号/3.0)