Search code examples
c#dynamicpropertiesbindable

Implementing a bindable object with dynamic properties


I am trying to make an object in C# that can be bound to a gridview. I have three objects to achieve this. EntryTable, EntryRow and EntryValue. An EntryTable contains a list of EntryRows and an EntryRow contains a list of EntryValues. An Entry value object consists of an attribute name and a value.

The data structure is as follows:

public class EntryTable 
{
    private List<EntryRow> _lERTable = null;
    ...

public class EntryRow
{
    private List<EntryValue> _lEVRow = null;
    ...

public class EntryValue
{
    private string _sFieldName;
    private string _sValue;
    ...

I am aware that bindable object by default will only bind to the top level visible properties. As a result I only see a list of the EntryRow object names like so:

-------------------------------------------
Norseman.CommandN.Data.CNStore.EntryRow    |
-------------------------------------------
Norseman.CommandN.Data.CNStore.EntryRow    |
------------------------------------------

My end goal is to be able to bind a populated EntryTable to a grid and have the EntryTable object recognize all of the attribute names in all of the EntryRows and display the data as a grid. The catch is that not every EntryRow will have the same number of EntryValues. For example, if I was storing employees, some employees may not have the manager attribute.

I wish to handle the data like so:

First Name |    Last Name  |     Position             |     Manager
-----------------------------------------------------------------
Andrew     |    Milton     |    Software Developer    |     null
-----------------------------------------------------------------
Barry      |   Singleton   |    Web Developer         |     Marcus Manicotti

I've considered entering my data into the traditional System.Data.DataTable model but I would prefer to implement my own object to avoid the overhead.

Any help in this matter would be greatly appreciated.


Solution

  • If you're using WinForms, you can implement ITypedList on the collection. If you're using WPF, you can use ICustomTypeDescriptor on each item.