C # / WPF: DataGrid Toolkit - Transposing Rows and Columns

I have

List<DetailObject> someList; 

which is as follows:

  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 changes, he will show me “Titel” as a ColumnHeader and the value of Titel as a row, dito for “Value1” as a ColumnHeader and the value (s) of Value1 as a string, etc.

Thanks for any help!

Greetings

EDIT: For a 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 am 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 a good approach here: http://codemaverick.blogspot.com/2008/02/transpose-datagrid-or-gridview-by.html

+6
c # wpf datagrid columnheader
source share
4 answers

You can use RowHeaderTemplate as follows:

 <toolkit:DataGrid> <toolkit:DataGrid.RowHeaderTemplate> <DataTemplate> <TextBlock Text="{Binding Item.Titel, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type toolkit:DataGridRow}}}"/> </DataTemplate> </toolkit:DataGrid.RowHeaderTemplate> </toolkit:DataGrid> 

You will also need to set AutoGenerateColumns to false and create your own columns so that the Titel property also appears as a column.

Edit

Now I understand that you want to migrate a DataGrid . I think an easy but somewhat hacky solution is to create a list of transposed objects. To do this, you need to know in advance how many objects are in the original list, and then create a new class with as many properties as there are objects.

 class TransposedDetailObject { public String Column1 { get; set; } public String Column2 { get; set; } public String Column3 { get; set; } } var transposedList new List<TransposedDetailObject> { new TransposedDetailObject { Column1 = someList[0].Titel, Column2 = someList[2].Titel, Column3 = someList[3].Titel }, new TransposedDetailObject { Column1 = someList[0].Value1, Column2 = someList[2].Value1, Column3 = someList[3].Value1 }, ... }; 

A less “hacky” solution is to change the DataGrid control template to replace rows and columns. However, a DataGrid is a complex control, and it can be a little overwhelming to change the control pattern.

+3
source share

This thread is a bit old, but maybe someone is still interested ... I was also looking for a way to port WPF DataGrid, and finally I found the following open source project in CodePlex:

Translated WPF DataGrid

Hope this helps.

+5
source share

Convert the list of business objects to datatable, where you created the columns from the properties of the object, and set this as the datagrid data source. If you want to enable editing, you will have to iterate over the data and manually apply the values ​​to your business objects. reflection can help make the grid common. just some thoughts.

+3
source share

I think a cleaner approach is to use an ItemsControl instead of a DataGrid for your script.

Follow the directions of this blog post and create your own ItemTemplate for maximum control. This way you can create a template for each column (which may be Datagrid itself).

+1
source share

All Articles