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?

+1


a source to share


2 answers


Dynamic columns can be added to ListView using Attached Properties. Check out this article on CodeProject , it explains exactly what ...



WPF DynamicListView - Binding to DataMatrix

+1


a source


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)});
}

      

+2


a source







All Articles