I've a
List<DetailObject> someList;
which looks like this:
public class DetailObject
{
public string Titel { get; set; }
public int Value1 { get; set; }
public int Value2 { get; set; }
public int Value3 { get; set; }
}
Does anyone know how I can use (with DataGrid.AutoGenerateColumns="True") the value of 'string Titel' as RowHeader and other members as "Row" Content? Without any modifications, it'll show me "Titel" as ColumnHeader and the value of Titel as row, dito for "Value1" as ColumnHeader and the value(s) of Value1 as Rows etc.
Thanks for any help!
Cheers
EDIT: For better understanding, this is what I have
[Titel] [Value1] [Value2] [Value3]
[Item1.Titel] [Item1.Value1] [Item1.Value2] [Item1.Value3]
[Item2.Titel] [Item2.Value1] [Item2.Value2] [Item2.Value3]
[Item3.Titel] [Item3.Value1] [Item3.Value2] [Item3.Value3]
and this is what I'm looking for:
[Item1.Titel] [Item2.Titel] [Item3.Titel]
[Item1.Value1] [Item2.Value1] [Item3.Value1]
[Item1.Value2] [Item2.Value2] [Item3.Value2]
[Item1.Value3] [Item2.Value3] [Item3.Value3]
EDIT2: I found also a nice approach here: http://codemaverick.blogspot.com/2008/02/transpose-datagrid-or-gridview-by.html
Convert the list of business objects to a datatable where you have created the columns from your object's properties and set that as your datagrid's itemsource. If you want to enable editing, then you'll have to iterate the datatable and manually apply the values back to your business objects. reflection may help make the grid generic. just some thoughts.
I think more clean approach is to use
ItemsControl
instead ofDataGrid
for your scenario.Follow the directions on this blog post and create your own
ItemTemplate
for maximum control. This way, you can create a template for each column (which can be a Datagrid itself).This thread is a bit old but maybe someone is still interested ... I was also looking for way to transpose a WPF DataGrid and finally I found the following open source project on CodePlex:
Transposed WPF DataGrid
Hope this helps.
You can use
RowHeaderTemplate
like this:You will also have to set
AutoGenerateColumns
tofalse
and create your own columns to avoid theTitel
property also being displayed as a column.Edit
I now understand that you want to transpose the
DataGrid
. I think the easy but somewhat "hacky" solution is to create a list of "transposed" objects. To do this you would have to know in advance how many objects there are in the original list and then create a new class with as many properties as there are objects.A less "hacky" solution is to modify the control template of the
DataGrid
to swap rows and columns. However,DataGrid
is a complex control and it can be a bit overwhelming to modify the control template.