Help displaying data on datagrid

  • Thread starter Thread starter Michael Gorbach
  • Start date Start date
M

Michael Gorbach

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.
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.
 
Hi,
[Inline]

Michael Gorbach said:
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.

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; }
}

// .......
// 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top