I have a Windows Forms TreeGrid, populated via reflection applied on some bussiness classes I wrote. Each line of the TreeGrid shows my class' properties, along with a column where I can edit the valueof the property.
My problems arise when I have "public xxx[]" on my class. I can populate the tree via code with any number of occurrences of "xxx" object, using buttons "Add" and "Delete". This thing works fine.
But the question is:
How I use reflection to populate my objects from the values of the tree when this objects are arrays, declared but not initialized im my bussiness classes?
When I finish this code, I will post it under an article here in CP.
Any hints or help?
TIA
Nilo
Brazil
UPDATE: The significant portion of my code below:
private void getProperties(Object obj, ref TreeGridNode origin)
{
int qtyItems = 0;
string value = "";
PropertyInfo[] propertyInfos;
TreeGridNode node;
bool dynamic = false;
propertyInfos = obj.GetType().GetProperties();
foreach (PropertyInfo p in propertyInfos)
{
if (!p.CanWrite)
continue;
if (p.Name.Equals("DeviceID"))
continue;
qtyItems = 1;
dynamic = false;
if (!p.PropertyType.IsArray || p.PropertyType.ToString().Equals("System.Byte[]"))
{
if (p.GetValue(obj, null) != null)
value = p.GetValue(obj, null).ToString();
}
else
{
if (p.GetValue(obj, null) != null)
{
Array arr = p.GetValue(obj, null) as Array;
qtyItems = arr.Length;
}
else
{
qtyItems = 0;
dynamic = true;
}
}
node = origin.Nodes.Add(p.Name, (dynamic?"":p.PropertyType.ToString()), (qtyItems == 1 ? value : ""), (qtyItems == 1 ? true : false), qtyItems, p, obj, dynamic,false);
if (qtyItems > 1)
{
for (int i = 1; i <= qtyItems; i++)
{
node.DefaultCellStyle.ForeColor = Color.Red;
Array ar = (Array)p.GetValue(obj, null);
TreeGridNode subRoot = node.Nodes.Add(p.Name + " " + i, "", "", false, 1, p.GetValue(obj, null), ar, ar, false,false);
this.getProperties(ar.GetValue(i - 1), ref subRoot);
}
}
}
}