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

Binding a column to a dictionary key

1 Answer 259 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mathew
Top achievements
Rank 1
Mathew asked on 20 Feb 2012, 04:58 PM
I've been searching the documentation and the forum for a while for this, but haven't been able to find any solid information...

I have an object which contains some fields and a dictionary - I want to bind each value in the dictionary to a column in a radgrid.  I don't know what these keys/values will be until the item comes back, and they'll change depending on the source of the query.

I can create the columns dynamically without a problem, but I need a way to bind their DataMember to the dictionary...what's the best way to achiever this?

class ItemQueryData
{
    public int ItemId { get; set; }
    public String SomeProperty { get; set; }
    public Dictionary<int, Object> Values;
}

private void AddRefColumns()
{
    foreach (KeyValuePair<int, String> f in HeaderColumns)
         _grid.Columns.Add(new GridBoundColumn() { DataField = "Values[" + f.Key + "]", HeaderText = f.Value });
}

1 Answer, 1 is accepted

Sort by
0
Mathew
Top achievements
Rank 1
answered on 21 Feb 2012, 10:30 AM
Just in case anyone else is trying to do this, it's possible to do by having your custon class implement the ICustomTypeDescriptor interface.  Then you can write your own GetProperties() method to determine which properties are returned by the class, and these can be used for databinding.  There are a few good tutorials around to get you started.
Tags
Grid
Asked by
Mathew
Top achievements
Rank 1
Answers by
Mathew
Top achievements
Rank 1
Share this question
or