1

I have data in a sql table that looks like this:

Name    colno    remark
----    -----    ------
bill    col 1    good
bill    col 2    ok
bill    col 3    triff
bill    col 4    A1
bob     col 1    poor
bob     col 2    excellent
bob     col 3    ok
bob     col 4    B+
bert    col 1    
bert    col 2    no info
bert    col 3    pass
bert    col 4    C-

and I want a gridview (?) with databinding that's laid out like this:

Name     col 1    col 2      col 3    col 4
Bill     good      ok        triff     A1
Bob      poor     excellent  ok        B+
bert              no info    pass      C-

So I can edit/insert data as needed.

The killer is that the number of columns can vary, eg I've 4 in the first example above, but it can be any number between 10 - 15. Hence on a second extract the data might look like this:

Name    colno    remark
----    -----    ------
brenda  col 1    ok
brenda  col 2    ok
brenda  col 3    excellent
boris   col 1    poor
boris   col 2    ok
boris   col 3    ok
baz     col 1    great
baz     col 2    no info
baz     col 3    ok

Giving a table like this:

Name     col 1    col 2      col 3
brenda   ok        ok        excellent
boris    poor      ok          ok
baz      great    no info      ok

I've explored sql pivot but I don't want the data summarised in any way, it's just a single field of text per record. So I don't think this kind of query will work:

select * from myData
pivot (someAggregateFunctionThatIDontNeed(colNo) for colNo_ButNotBeingAggregated in (variableNoOfColumns)) as notAnAvergageJustText

I've tried to build a DataTable piecemeal from multiple sql-extracts-into-datatables-then-loop (that works, kinda, but I can't figure out how to bind the data after)

Any ideas? I've caned this for four days and I'm stumped. The offer of the wifey coming round to cook your Sunday dinner is still on...

4

2 回答 2

0

您是否尝试过在 SQL 语句中使用 ISNULL 来检查没有数据的列?如果没有数据,您可以在语句本身中设置默认设置。

ISNULL([YOUR FIELD],'Whatever you want to display here')
于 2013-01-21T21:03:04.517 回答
0

你需要一个支点。查找 SQL 的“pivot”MSDN,或谷歌它。准备花一点时间了解它,语法并不简单。

于 2013-01-19T21:31:27.193 回答