In WPF, can you bind a ListView (NOT DataGrid) to a Matrix dataset (Cross-Tab)?
Is it possible in WPF to bind a ListView (NOT DataGrid) to a Matrix (Cross-Tab) dataset where the columns are unknown in advance?
Using the Northwind database as an example, the simple query below will return a well-known dataset that can be easily bound to a list:
SELECT Year(o.OrderDate) AS [Year], Month(o.OrderDate) AS [Month],
COUNT(o.OrderID) AS [NumOrders]
FROM Orders o
GROUP BY Year(o.OrderDate), Month(o.OrderDate)
ORDER BY 1, 2
alt text http://i43.tinypic.com/rlhrh0.png
To generate a more meaningful report, we can run the result set from the above query (in the reporting program) to generate something like below:
SELECT [Year],
SUM(CASE [Month] WHEN 1 Then NumOrders ELSE 0 END) AS 'Jan',
SUM(CASE [Month] WHEN 2 Then NumOrders ELSE 0 END) AS 'Feb',
SUM(CASE [Month] WHEN 3 Then NumOrders ELSE 0 END) AS 'Mar',
SUM(CASE [Month] WHEN 4 Then NumOrders ELSE 0 END) AS 'Apr',
SUM(CASE [Month] WHEN 5 Then NumOrders ELSE 0 END) AS 'May',
SUM(CASE [Month] WHEN 6 Then NumOrders ELSE 0 END) AS 'Jun',
SUM(CASE [Month] WHEN 7 Then NumOrders ELSE 0 END) AS 'Jul',
SUM(CASE [Month] WHEN 8 Then NumOrders ELSE 0 END) AS 'Aug',
SUM(CASE [Month] WHEN 9 Then NumOrders ELSE 0 END) AS 'Sep',
SUM(CASE [Month] WHEN 10 Then NumOrders ELSE 0 END) AS 'Oct',
SUM(CASE [Month] WHEN 11 Then NumOrders ELSE 0 END) AS 'Nov',
SUM(CASE [Month] WHEN 12 Then NumOrders ELSE 0 END) AS 'Dec'
FROM
(
SELECT Year(o.OrderDate) AS [Year], Month(o.OrderDate) AS [Month],
COUNT(o.OrderID) AS [NumOrders]
FROM Orders o
GROUP BY Year(o.OrderDate), Month(o.OrderDate)
)t0
GROUP BY [Year]
Which leads to something like this:
alt text http://i44.tinypic.com/33kpt7q.png
My question is, is it possible to bind this end result to a ListView or Flowdocument in WPF without prior knowledge of the resulting columns?
a source to share
You can generate GridViewColumns for the ListView programmatically and apply binding to that. Ignore the columns in the DataSet and add the appropriate GridViewColumn to the ListView.
var gridView = (GridView)list.View;
foreach(var col in table.Columns) {
gridView.Columns.Add(new GridViewColumn{
Header=col.ColumnName,
DisplayMemberBinding=new Binding(col.ColumnName)});
}
a source to share