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

Data Binding / MVVM with unknown (in advance) columns

8 Answers 135 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Tim
Top achievements
Rank 1
Tim asked on 12 Nov 2010, 03:47 PM
I have seen a bunch of queries here about binding to Hashtables or Dictionaries.  My problem, much like others, is that I don't know in advance what the columns will be, and I get back a collection (rows) of Dictionary (column = key, value = cell) from a web service, rather than a collection of objects with properties.

Imagine a generic grid that displays election results.  The rows could be the year, and the columns could be the candidate names and the cells could be the number of votes.  Since I do not control the query itself, I need to be prepared to handle an arbitrary set of values.  Ideally I would like to say AutoGenerateColumns=True and ItemsSource={Binding SomeEnumerableCollectionOfDictionaries} so that there is no code-behind.  I want sorting and filtering.

Creating the columns by hand (in code) will also require creating the cells by hand, since I haven't got an object with properties to bind to, even if I can figure out the column names (from the keys) in advance.

Pointers or suggestions are most welcome.

Thanks,
Tim

8 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 15 Nov 2010, 08:29 AM
Hello Tim,

 Why not use our lightweight DataTable? Please check also this blog post

Greetings,
Vlad
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Tim
Top achievements
Rank 1
answered on 16 Nov 2010, 02:50 PM
How does one go about binding to an

ObservableCollection<T> MyItemsSource

when you do not know what type T is?  Your blog example works, but it does not bind the GridView to an observable collection and it isn't very MVVC-like.

Thanks,
Tim
0
Tim
Top achievements
Rank 1
answered on 30 Nov 2010, 10:29 PM
How does your example deal with ColumnName containing a . (period) character?

table.Columns.Add(new DataColumn() { ColumnName = "Files matching *",  DataType = typeof(MyCustomType) });

works, but

table.Columns.Add(new DataColumn() { ColumnName = "Files matching *.pdf",  DataType = typeof(MyCustomType) });

does not.  I have reduced it to the "." being the problem.  I am now stuck wondering what other restrictions there are on column/property names.  Consider filtering a file system and you can imagine that people might create filters named for the file patterns they filter - I.e., *.pdf, which cannot become a column header in this example.

Help!  I'm facing a deadline of today... with a few more days to fix bugs.

Thanks,
Tim

0
Tim
Top achievements
Rank 1
answered on 30 Nov 2010, 10:46 PM
Additionally, sorting causes the app to die:

Unhandled Error in Silverlight Application Code: 4004 Category: ManagedRuntimeError Message: System.ArgumentException: Invalid property or field - 'pdf' for type: MyCustomType at Telerik.Windows.Data.Expressions.MemberAccessTokenExtensions.CreateMemberAccessExpression(IMemberAccessToken token, Expression instance) at Telerik.Windows.Data.Expressions.ExpressionFactory.MakeMemberAccess(Expression instance, String memberName) at Telerik.Windows.Data.Expressions.ExpressionFactory.MakeMemberAccess(Expression instance, String memberName, Boolean liftMemberAccessToNull) at
...

so it must be using the "." to look for properties...

Tim
0
Tim
Top achievements
Rank 1
answered on 30 Nov 2010, 11:36 PM
Here is a "worst-case" scenario kind of program (changes to the example):

private string[] props = new string[]
                             {
                                 "ID",
                                 "ID ",
                                 "I D",
                                 "I D ",
                                 " I D ",
                                 ".",
                                 "..",
                                 "*",
                                 "**",
                                 "*.pdf",
                                 ".pdf",
                                 ".txt.pdf",
                             };
 
public Page()
{
    InitializeComponent();
 
    table = new DataTable();
 
    foreach (string prop in props)
    table.Columns.Add(new DataColumn() { ColumnName = prop, DataType = typeof(string) });
 
    for (var i = 0; i < 10; i++)
    {
        DataRow row = table.NewRow();
        UpdateRowValues(row, i);
 
        table.Rows.Add(row);
    }
 
    RadGridView1.ItemsSource = table;
}
 
private void UpdateRowValues(DataRow row, int i)
{
    foreach (string prop in props)
    {
        row[prop] = string.Format("{{{0}}} {{{1}}}", prop, i);
    }
}


Tim
0
Tim
Top achievements
Rank 1
answered on 04 Dec 2010, 09:19 PM
I have worked around this by manufacturing unique property names and adding a translation layer to get back the invalid property names.

Tim
0
Brian
Top achievements
Rank 1
answered on 18 Oct 2011, 10:35 PM
Is a fix expected anytime soon to allow column headers that start with a number OR periods (.) within column headers?  We have also run across this issue with other characters as well.  Seems like a bug to me.
0
Vlad
Telerik team
answered on 19 Oct 2011, 07:10 AM
Hello,

You can check this blog post for more info about how to achieve the same with dynamic objects:
http://blogs.telerik.com/vladimirenchev/posts/11-09-28/dynamic-binding-for-your-silverlight-applications.aspx

All the best,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
GridView
Asked by
Tim
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Tim
Top achievements
Rank 1
Brian
Top achievements
Rank 1
Share this question
or