![](/img/trans.png)
[英]Dynamic DataGridView creation columns and rows not coming in winform using c#
[英]How to C# WinForm datatable loop through rows and columns to datagridview
SQL查詢返回
cid Sid SalesName Ratio Price Amount
1 01 Mike 11 80 120
1 11 Salier 10 70 90
3 04 Amy 8 60 200
3 01 Mike 25 110 600
我希望它在datagridview或sql查詢中顯示如下
cid Mike_Price Mike_Amount Balance Salier_Price Salier_Amount Balance Amy_Price Amy_Amount
1 80 120 40 70 90 20 0 0
3 110 600 490 0 0 0 60 200
為了顯示您想要的內容,您必須編寫查詢以返回第二條語句。 從您的桌子看來,這是不可能的。
如果使用的是SQL Server(> 2005),則可以考慮在SQL方面做一個透視。 通用語法為:
SELECT
[non-pivoted column], -- optional
[additional non-pivoted columns], -- optional
[first pivoted column],
[additional pivoted columns]
FROM (
SELECT query producing sql data for pivot
-- select pivot columns as dimensions and
-- value columns as measures from sql tables
) AS TableAlias
PIVOT
(
<aggregation function>(column for aggregation or measure column) -- MIN,MAX,SUM,etc
FOR [<column name containing values for pivot table columns>]
IN (
[first pivoted column], ..., [last pivoted column]
)
) AS PivotTableAlias
ORDER BY clause -- optional
這將以您要查找的格式創建數據,並使顯示直接。
這是一個很好的教程:
http://www.kodyaz.com/articles/t-sql-pivot-tables-in-sql-server-tutorial-with-examples.aspx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.