SQL语句那么写是正确的。如果那么做看不懂,可以先把所有数据取到DataTable里,然后在C#里通过循环构造你要的表。至于显示时用TextBox,可以在GridView中用TemplateItem阿。

解决方案 »

  1.   

    到SQL板块,那有很多列行转换的方法
      

  2.   


    create  table #tbl_0
    (
       color nvarchar(12),
       nsize nchar(1),
       store int
    )
    insert into #tbl_0
    select 'red',    'S',    '10' union all 
    select  'red',    'M',    '80'  union all
    select 'white',    'L',    '50'  union all
    select  'white',    'S',    '60' select * from #tbl_0
    --结果
    --red S 10
    --red M 80
    --white L 50
    --white S 60
    declare @sql varchar(4000)
    set @sql = ' select color,'
    select @sql= @sql+'isnull(sum(case nsize when '''+ns+''' then isnull(store,0) end),0) '''+ns+''','
    from (select 'S' as ns union all select 'M' union all select 'L') a
    set @sql = stuff(@sql,len(@sql),1,'')
    set @sql=@sql+ ' from #tbl_0 group by color'exec(@sql)--结果
    --red 10 80 0
    --white 60 0 50drop table #tbl_0
      

  3.   

    其实动态sql就是转成这样
    select color,
    isnull(sum(case nsize when 'S' then isnull(store,0) end),0) 'S',
    isnull(sum(case nsize when 'M' then isnull(store,0) end),0) 'M',
    isnull(sum(case nsize when 'L' then isnull(store,0) end),0) 'L'
     from #tbl_0 group by color
      

  4.   

    1楼的方法我知道,我的购物车就是这样做的,但是在这里实现起来不容易,实在做不出来我就采用那个方法试试。
    4楼CutBug的方法可行,先谢了,我还有些问题要请教,如果不够分我再加。
    问题1:显示结果如下:
    颜色 S   M    L     
    红色 10  80   0 
    白色 60  0    50    但是,我绑定GridView的时候,在编辑列里我是设定了"自动生成字段"打勾,如果我是设定了某个特定的字段要怎么设定? 我搞了很久不明白,BoundField的DataField应该绑定什么?或者是用TemplateField的时候添加TextBox的时候TextBox绑定什么? Eval("里面写什么?")问题2 :假如上面的问题搞定了,如果我要加一个Button控件,怎么将TextBox控件里的数据修改后点击Button控件就保存到数据库里? 请高手们指教,谢谢!