views:

3027

answers:

2

Hi,
I have a list of string array as input. The array dimension are static for all the list but I can't know the array size until the list is retrieved. I need to bind the list in a datagrid in Silverlight.

I tryied to create columns at runtime, binding each column to a particular array position but cannot find a way.

Have you any idea how to do it?

I could eventually "convert" my list of array in anything else that could be binded in a datagrid?

The idea is to have a sort of 2d Matrix showed inside the datagrid in Silverlight (I think that the problem is similar).

List

 column_1      column_2       column_3   ..   column_m

string[1,1]   string[1,2]    string[1,3] ..  string[1,m]
string[2,1]   string[2,2]    string[2,3] ..  string[2,m]
string[3,1]   string[3,2]    string[3,3] ..  string[3,m]
....
string[n,1]   string[n,2]    string[n,3] ..  string[n,m]

n is list lenght, m is list column max number.

Any (and when say any I mean any) idea is appreciated

Thanks in advance

Giorgio

+1  A: 

Unfortunately that's not gonna be easy.

Do you have any valid constraints, like the maximum number of columns that is allowed or anything like that? If you do (let's say you have N column maximum), you might be able to do something by

  1. Having a class that exposes N properties (named Col1...ColN for example) that map the content of the array for one line at column X
  2. Generating a list of that class, one instance for each line
  3. Generating the correct number of column on the fly, binding each column to property ColX
  4. Binding your DataGrid to that list

That's kind of ugly, but it would work.

If you do not have to rely on the DataGrid, there is a possibility using a UniformGrid. A UniformGrid is a panel that layouts its children in a regular grid (every item has the same width, every item has the same height). You can indicate the number of columns at runtime, and the panel will fit children one after the other up to the number of columns and then continue on another line. You could bind an ItemsControl to your array, indicating it should use a UniformGrid as its layout panel and indicating a suitable ItemTemplate to render each string.

The second option is much easier, but you will not have the capabilities of the DataGrid like sorting, moving columns, row selection, edition events per row etc.

Denis Troller
Thanks for anwer me. I'll try to use the first one.. unfortunately I need the built in behavior of the datagrid. I also found another answer on my question: http://silverlight.net/forums/t/13083.aspx. Thanks again
krumikaze
+2  A: 

Hi, I've found two solution to the problem that use the schema in the Denis's answer:

  • the first one is to use reflection to generate a class at runtime for the binding as suggested in this article (thanks to Vladimir Bodurov). I've tested this solution and I'll try to use it on my project. The only problem right now is that for large collection, the performance are poor. But I hope that someone will fix it in next relese (Silverlight 3 seems to not have fixed this problem, yet)

  • the second solution will be using some dynamic language for generate data. I don't know if this could be faster or not (probably not) but eventually could help. I will try in the future and use ironpython or ruby to generate classes that will be binded in datagrid.

If anyone have tryed using the second solution or any performance related information about creating classes at runtime, it will be appreciated.

Giorgio

krumikaze
That article by Vladimir is *awesome*... exactly what I needed for [SQL in Silverlight] http://conceptdevelopment.net/Silverlight2/SharpSql02/default.htmlI had to update Vladimir's code for generic Dictionary<string,string> posted here http://conceptdevelopment.net/Silverlight2/SharpSql02/DataSourceCreator.cs.html
CraigD