| re: Help displaying data on datagrid
Hi,
[Inline]
"Michael Gorbach" <mgorbach@gmail.com> wrote in message
news:1122306064.357550.29360@g43g2000cwa.googlegro ups.com...[color=blue]
> Iv got a StatisticsContainer object that contains an arraylist of
> objects of different types, all inherited from class Statistic. The
> statistics class has 2 string public properties, name and stringValue.
> The classes that inherit from it use those two properties, plus several
> properties of their own of types double and vector. I need to display
> this StatisticsContainer class on a datagrid, but I'm having problems
> because each of the classes have some different properties. The
> datagrid automatically binds to all public properties of the first
> entry, but most of these (all but stringValue), are actually unique to
> the first entry. What results if i add another entry to the arraylist
> is an exception from the reflection namespace. I need to do one of
> three things:
> 1. force the datagrid to display only the properties common to the
> statistic base class.[/color]
This is possible by implementing your own custom collection which implements
ITypedList :
using System.Reflection;
using System.ComponentModel;
using System.Collections;
public class StatisticsContainer : CollectionBase, ITypedList
{
public int Add( Statistic a )
{
return List.Add( a );
}
public Statistic this[int i]
{
get { return (Statistic)List[i]; }
}
// .......
// implement other collection properties the same way ( using List )
// .......
// implementation of ITypedList
public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[]
listAccessors)
{
if ( listAccessors == null || listAccessors.Length == 0 )
{
PropertyDescriptorCollection pdc = new
PropertyDescriptorCollection(null);
// Using reflection to keep the order of fields
// typeof( Base-Class-Name )
foreach( PropertyInfo pi in typeof(Statistic).GetProperties() )
{
// by default properties are included,
// unless they have the [BindableAttribute(false)].
object[] o = pi.GetCustomAttributes(typeof(BindableAttribute),
false );
if ( o.Length == 0 || ((BindableAttribute)o[0]).Bindable )
{
pdc.Add( TypeDescriptor.CreateProperty( typeof(Statistic),
pi.Name, pi.PropertyType ) );
}
}
return pdc;
}
else
{
// only required for hierarchical collections, not implemented,
// ask if you need help with this
return null;
}
}
// implementation of ITypedList
public string GetListName(PropertyDescriptor[] listAccessors)
{
if ( listAccessors == null || listAccessors.Length == 0 )
{
return this.GetType().Name;
}
else
{
// only required for hierarchical collections, not implemented,
// ask if you need help with this
return null;
}
}
}//end-class
HTH,
Greetings
[color=blue]
> or
> 2. have the datagrid display little plus marks allowing me to expand
> each entry and see all of this unique properties.
>
> or
>
> 3. (the best) do both.
>
> The datagrid control is completely confusing me with its myriad
> collections of styles. I have no reference books (beyond what i can
> find on the web), so I would greatly appreciate some help.
>[/color] |