IList<IList<string>> as DataGrid ItemsSource?

Jan 30, 2009 at 4:50 PM
Is it possible to have IList<IList<string>> bound to a DataGrid's ItemsSource, where the IList<string> items of the outer list describes the rows of the DataGrid?

Thanx!
Feb 5, 2009 at 1:42 PM
would be nice to know. if someone knows the answer..I m looking forward to se the answer..cheers
Feb 5, 2009 at 3:27 PM
Hi,

Yes, you can. Here is an example:

<dg:DataGrid ItemsSource="{Binding}" Name="dataGrid" AutoGenerateColumns="False">
    <dg:DataGrid.Columns>
        <dg:DataGridTextColumn Binding="{Binding Path=[0]}"/>
        <dg:DataGridTextColumn Binding="{Binding Path=[1]}"/>
        <dg:DataGridTextColumn Binding="{Binding Path=[2]}"/>
    </dg:DataGrid.Columns>
</dg:DataGrid>

which is bound to ....

 List<List<String>> data = new List<List<string>>(){
                new List<string>()
                {
                    "cat", "dog", "fish"
                },
                new List<string>()
                {
                    "chicken", "cow", "moose"
                }
            };

Note, that you cannot AutoGenerate columns because the property descriptor for List will only detail the list's Count and Capacity properties. If you want to AutoGenerate columns, you will have to write your own property descriptor.

Regards,
Colin E.
--
http://www.scottlogic.co.uk/blog/wpf/ - my WPF blog
http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx - WPF DataGrid Practical Examples


Feb 5, 2009 at 7:14 PM
How do you do this in C# code:

<dg:DataGrid ItemsSource="{Binding}" Name="dataGrid" AutoGenerateColumns="False">
    <dg:DataGrid.Columns>
        <dg:DataGridTextColumn Binding="{Binding Path=[0]}"/>
        <dg:DataGridTextColumn Binding="{Binding Path=[1]}"/>
        <dg:DataGridTextColumn Binding="{Binding Path=[2]}"/>
    </dg:DataGrid.Columns>
</dg:DataGrid>
Feb 6, 2009 at 7:39 AM
Try this ...

dataGrid.Columns.Add(new DataGridTextColumn()
            {
                Binding = new Binding("[0]")
            });

(rinse and repeat)
Regards,
Colin E.
--
http://www.scottlogic.co.uk/blog/wpf/ - my WPF blog
http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx - WPF DataGrid Practical Examples