views:

1345

answers:

2

Hello,

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

A: 

You can use RowHeaderTemplate like this:

<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 have to set AutoGenerateColumns to false and create your own columns to avoid the Titel 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.

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 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.

Martin Liversage
Thanks for the response. I tried this out, but it will only show the Item.Titel as Row. What I want is to have each Item.Titel (from the List someList) as a Column and all infos of it (e.g. Item.Value1) below in a Row/Cell. I'll create a "mockup" of what I mean in the question :)
Joseph Melettukunnel
Regarding the edit: Chaning the DataSource is an option, I'll have to think about how to create a class which can handle a dynamic set of "Columns", since the original list is variable. Thanks for your help and input. If I find a solution, I'll post it here. I'll leave the question open until tomorrow, maybe someone else has an idea - if not, I'll mark your answer as correct.
Joseph Melettukunnel
You can create a class with a lot of properties and then programmatically modify the `DataGrid` to only show as many columns as original objects. In .NET 4 you also have the option of creating a dynamic class at run-time.
Martin Liversage
A: 

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.

steve s