Data Binding / MVVM with unknown (in advance) columns

9 posts, 0 answers
  1. Tim
    Tim avatar
    169 posts
    Member since:
    Apr 2008

    Posted 12 Nov 2010 Link to this post

    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
  2. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 15 Nov 2010 Link to this post

    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
  3. Tim
    Tim avatar
    169 posts
    Member since:
    Apr 2008

    Posted 16 Nov 2010 Link to this post

    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
  4. Tim
    Tim avatar
    169 posts
    Member since:
    Apr 2008

    Posted 30 Nov 2010 Link to this post

    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

  5. Tim
    Tim avatar
    169 posts
    Member since:
    Apr 2008

    Posted 30 Nov 2010 Link to this post

    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
  6. Tim
    Tim avatar
    169 posts
    Member since:
    Apr 2008

    Posted 30 Nov 2010 Link to this post

    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
  7. Tim
    Tim avatar
    169 posts
    Member since:
    Apr 2008

    Posted 04 Dec 2010 Link to this post

    I have worked around this by manufacturing unique property names and adding a translation layer to get back the invalid property names.

    Tim
  8. Brian
    Brian avatar
    73 posts
    Member since:
    Sep 2010

    Posted 18 Oct 2011 Link to this post

    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.
  9. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 19 Oct 2011 Link to this post

    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 >>

Back to Top