let’s have these classes:
public class MyObject
{
//constructor and other stuff
public bool MyBool { get; set; }
public string MyString { get; set; }
public double MyDouble { get; set; }
...
public List<MyProperty> Properties { get; set; }
}
public class MyProperty
{
//constructor and other stuff
public Type TheType { get; set; }
public String Name { get; set; }
public Object Value { get; set; }
}
In the form I will populate the values:
MyObject myObject = new MyObject(true, "Foo", 12);
MyObject myObject2 = new MyObject(false, "Bar", 16);
myObject.Properties.Add(new MyProperty(typeof(double),"Amount",2000));
myObject.Properties.Add(new MyProperty(typeof(string),"Name","Ale"));
//in the same order, with the same type and name
//only the values are different
myObject2.Properties.Add(new MyProperty(typeof(double),"Amount",3000));
myObject2.Properties.Add(new MyProperty(typeof(string),"Name","Teru"));
List<MyObject> list = new List<MyObject>();
list.Add(myObject);
list.Add(myObject2);
Now, the question is: how can I display the fixed fields and the dynamic defined properties in a single datagridview?
The final result should be something like:
MyBool | MyString | MyDouble | Amount | Name
true | Foo | 12 | 2000 | Ale
false | Bar | 16 | 3000 | Teru
Any ideas or hints are welcome.
The only way that we have ever been able to do this is using a third party grid control that allows us to define columns at run time and populate the values in these columns on a row-by-row basis. I haven’t tried this with a standard grid, but hopefully the same principle applies.
Basically, the process at runtime is:
1) Add the standard columns using the fixed properties and databind them to the underlying property.
2) Add the columns necessary to display the dynamic properties, but don’t databind them.
3) On display, for each row, move the content from the underlying record into the row.
4) On save, for each row, move the content from the row back into the record.
Although this sounds like a lot of work, once you have it figured out, it it pretty easy.