views:

497

answers:

1

This is a follow up question from an earlier post (here).

I have some 'header' information stored as: Dictionary<string,string> - where the first string represents the field name, and the second the heading I want displayed.

I have a set of dynamic data that is stored as: Dictionary<string, object> - where string is the field name.

I bind to this in xaml as:

<data:DataGrid Name="_dataGrid" AutoGenerateColumns="True"  IsReadOnly="False" Margin="5" Height="200">
            <data:DataGrid.Columns>
                <data:DataGridTextColumn Header="Forename" CanUserSort="True" SortMemberPath="Forename" 
                                          Binding="{Binding Converter={StaticResource RowIndexConverter},
                                            ConverterParameter=Forename}"/>
                <data:DataGridTextColumn Header="Surname" CanUserSort="True" SortMemberPath="Surname" 
                                         Binding="{Binding Converter={StaticResource RowIndexConverter},
                                            ConverterParameter=Surname}"/>
                <data:DataGridTextColumn Header="Age" CanUserSort="True" SortMemberPath="Age" 
                                         Binding="{Binding Converter={StaticResource RowIndexConverter},
                                            ConverterParameter=Age}"/>
                <data:DataGridTextColumn Header="Shoesize" CanUserSort="True" SortMemberPath="Shoesize" 
                                         Binding="{Binding Converter={StaticResource RowIndexConverter},
                                            ConverterParameter=Shoesize}"/>                
            </data:DataGrid.Columns>
        </data:DataGrid>

Problem 1 I want to autogenerate these columns (using the header information supplied)

Problem 2 I want the columns to be generated based on what datatype they are (i.e. boolean = checkbox)

Problem 3 Ideally I would also like to specify weather a button should exist in the first column or not (i.e. an edit / view button) via databinding

+1  A: 

I've used an approach that follows the pattern of this pseudocode

columns = New DynamicTypeColumnList()
columns.Add(New DynamicTypeColumn("Name", GetType(String)))
dynamicType = DynamicTypeHelper.GetDynamicType(columns)

DynamicTypeHelper.GetDynamicType() generates a type with simple properties. See this post for the details on how to generate such a type

Then to actually use the type, do something like this

Dim rows as List(Of DynamicItem)
Dim row As DynamicItem = CType(Activator.CreateInstance(dynamicType), DynamicItem)
row("Name") = "Foo"
rows.Add(row)
dataGrid.DataContext = rows
qntmfred
clearly there's a lot of details left out of this explanation, but it should set you on the right path.
qntmfred
Ahh, yes - I have looked into Vladimir Bodurov's method before. And it is really cool. However, my issue with it that the dynamic type generation means you cannot use strong typing at design time (and run-time debuging returns null's). Colin has a good post here: http://www.scottlogic.co.uk/blog/colin/2009/04/binding-a-silverlight-datagrid-to-dynamic-data-via-idictionary/comment-page-1/#comment-5535 Which explains a more 'standard' way of doing things. It requires some tweaking when using with different controls, but the c# code is a very elegant solution.
Grayson Mitchell