This is a migrated thread and some comments may be shown as answers.

Multidimensional array (or similar) as ItemsSource

16 Answers 958 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
BOARD LAB
Top achievements
Rank 1
BOARD LAB asked on 21 Apr 2009, 08:36 AM
I have a fairly stupid problem but I can't get around it: my data is in form of a multidimensional array, i.e.
string[,], or string[][], or even List<List<string>> where the first row contains the headers and each subsequent row the data.

I can't transform the data to a list of objects because I only know the columns at runtime.

Is there a way to bind a RadGridView to this kind of data model?

Thanks,
Francesco

16 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 21 Apr 2009, 03:28 PM
Hello Francesco,

You can use List<Dictionary<string, object>> as ItemsSource:

XAML
<UserControl x:Class="SilverlightApplication1.Page" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
    xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"
    <Grid> 
        <telerik:RadGridView x:Name="RadGridView1" IsReadOnly="True" AutoGenerateColumns="False" /> 
    </Grid> 
</UserControl> 


C#
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using System.Xml.Linq; 
using System.Collections; 
using System.Windows.Data; 
using Telerik.Windows.Controls; 
 
namespace SilverlightApplication1 
    public partial class Page : UserControl 
    { 
        public Page() 
        { 
            InitializeComponent(); 
 
            List<Dictionary<stringobject>> source = new List<Dictionary<stringobject>>(); 
 
            for (int i = 0; i < 100; i++) 
            { 
                Dictionary<stringobject> item = new Dictionary<stringobject>(); 
 
                for (int j = 0; j < 10; j++) 
                { 
                    item[String.Format("Column{0}", j)] = String.Format("Cell {0} {1}", i, j); 
                } 
 
                source.Add(item); 
            } 
 
            for (int i = 0; i < 10; i++) 
            { 
                string propertyName = String.Format("Column{0}", i); 
                GridViewDataColumn column = new GridViewDataColumn(); 
                column.DataMemberBinding = new Binding() { Converter = new MyConverter(), ConverterParameter = propertyName }; 
                column.HeaderText = propertyName; 
                RadGridView1.Columns.Add(column); 
            } 
 
            RadGridView1.ItemsSource = source; 
        } 
    } 
 
    public class MyConverter : IValueConverter 
    { 
        #region IValueConverter Members 
 
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
        { 
            return ((Dictionary<stringobject>) value)[(string) parameter]; 
        } 
 
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
        { 
            throw new NotImplementedException(); 
        } 
        #endregion 
    } 
 


All the best,
Vlad
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
BOARD LAB
Top achievements
Rank 1
answered on 21 Apr 2009, 03:32 PM
Thanks, this is a nice solution.
Do you think it will have a big negative impact on performance with a large number of rows/columns?

Thanks
Francesco
0
Vlad
Telerik team
answered on 21 Apr 2009, 03:36 PM
Hello Francesco,

I believe that you will not get negative impact on performance however the major problem with this approach is that the grid will unable to sort, group, filter, edit/update, etc.

Kind regards,
Vlad
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
BOARD LAB
Top achievements
Rank 1
answered on 22 Apr 2009, 02:57 PM
Thanks for the answer,

this is unfortunate as it means we can't use RadGridView in our project.
Do you plan to support this data model in future?

The use case scenario is quite common: we need to display data coming from a database query (not known in advance, i.e. we cannot map it to an object).
0
Vlad
Telerik team
answered on 23 Apr 2009, 06:58 AM
Hello Francesco,

This limitation comes directly from Silverlight Binding class - you cannot work normally with indexed properties. I believe that if you use ADO.NET DataService you will get desired result with minimal effort - you can check the application in this thread for more info:
http://www.telerik.com/community/forums/silverlight/gridview/load-on-demand-gridview.aspx

Kind regards,
Vlad
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
BOARD LAB
Top achievements
Rank 1
answered on 23 Apr 2009, 07:56 PM
Vlad,

I found a solution that works in our case. It's not very straightforward but it does the job. The idea is to create the type at runtime, add a property for each one of my column and then (using reflection) create a list of these objects with my data.

something like this:
myData = array[n columns][ i rows]
Type tempType = [create a type with n properties ("column_0", "column_1", etc.)]
foreach row in myData
  create an object of type TempType filling the properties column_0, column_1,etc. with myData[n][i], add the object to a list
set this list as itemsSource of the grid.

It works beautifully (and isn't even so slow), the only issue is that native sorting and grouping does not work, the grid seems to be unable to assign the CellItemType property (is this the right name? I don't have the code here right now), so it doesn't find the properties that define each column and cannot perform the sort/grouping. Probably it depends on the way the gridView looks for the properties in each row.
In practice the dataGrid thinks that each row is a System.Object instead of TempType, so it doesn't find the properties column_0, column_1, etc.

Sorting is not really a problem as I can use a custom sort, but for grouping I can't make it work.

Maybe if I post a small sample project you can give it a look and tell me if there is a way to perform grouping?

Thanks,
Francesco
0
Accepted
Vlad
Telerik team
answered on 24 Apr 2009, 06:16 AM
Hello Francesco,

Yesterday I've made small DataTable like class for Silverlight - please check this post for more info:
http://blogs.telerik.com/blogs/09-04-23/lightweight_datatable_for_your_silverlight_applications.aspx

The grid will sort, group and filter as expected.

Kind regards,
Vlad
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
BOARD LAB
Top achievements
Rank 1
answered on 24 Apr 2009, 06:49 AM
Vlad,
Chapeaux!

You saved my day. I was going a bit in the same direction as the Dynamic.cs class (with code.emit etc.), but this is taken to a whole new level.
Thanks!!!
0
BOARD LAB
Top achievements
Rank 1
answered on 24 Apr 2009, 01:31 PM
Vlad,
with your DataTable class I'm literally one small step away from a solution. What I'm missing is the ability to have a custom object in the datatable (I posted the question on your blog btw).
My values are not value type but I have a class that describes the content of each cell (with additional data that I need besides the value).
The DataTable throws an Exception when I fill it with a custom object. Do you think it's something that can be solved?
0
Accepted
Vlad
Telerik team
answered on 24 Apr 2009, 01:45 PM
Hi Francesco,

Indeed you can do this however please set DataType property for the column to typeof(object).

Let me know how it goes.

Greetings,
Vlad
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
BOARD LAB
Top achievements
Rank 1
answered on 24 Apr 2009, 03:08 PM
Problem is that if I set DataType=typeof(object) then the RadGridView does not allow sorting and grouping (I guess it looks for the properties with reflection on the DataType).
0
BOARD LAB
Top achievements
Rank 1
answered on 24 Apr 2009, 03:25 PM
Hold on, looks like it's working.
Maybe it stopped sorting/grouping for some different reason not related to the DataTable as it seems to be working right now. My bad, sorry.
0
Vlad
Telerik team
answered on 24 Apr 2009, 03:37 PM
Hi Francesco,

Generally the grid sorting will work by default only on known types however for example if you implement IComparable for your object and set IsCustomSortingEnabled to true for this column you will able to sort.

All the best,
Vlad
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
BOARD LAB
Top achievements
Rank 1
answered on 24 Apr 2009, 03:48 PM
I indeed implemented IComparable in my class. It seems to work even without customSorting=true, (I will check why, but hey, even better!).

Thanks for all the help!
0
Johnny
Top achievements
Rank 1
answered on 26 Oct 2009, 11:16 PM
Have you had any luck creating a vb version of the dynamic and datatable classes?
0
Vlad
Telerik team
answered on 27 Oct 2009, 06:05 AM
Hello Johnny,

VB.NET version of Dynamic LINQ can be downloaded here:
http://msdn.microsoft.com/en-us/bb964686.aspx

Sincerely yours,
Vlad
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
GridView
Asked by
BOARD LAB
Top achievements
Rank 1
Answers by
Vlad
Telerik team
BOARD LAB
Top achievements
Rank 1
Johnny
Top achievements
Rank 1
Share this question
or