Question

I'm binding IList to a GridView. IMyInterface looks like

public interface IMyInterface: IHasTotalHours, IHasLines
{
    DateTime GoalStartDate { get; set; }
    DateTime GoalEndDate { get; set; }
}

I bind an instance to a Grid like this:

IList<IMyInterface> instance= GetMyData();

myGrid.DataSource = instance;
myGrid.DataBind();

When bind this to the grid, the only members that show up in the grid are the direct members of IMyInterface: GoalStartDate and GoalEndDate.

Why is that? How do I get the grid to display the members of the other interfaces it inherits?

Update The inherited interfaces define simple data properties like

public interface IHasTotalHours
{
    string Description { get; set; }
    int Hours{ get; set; }
}
public interface IHasLines
{
    double TotalLines { get; set; }
    double LinesPerHour { get; set; }
}

There is a class that implements IMyInterface:

public class MyClass : IMyInterface
{
    public string Description { get; set; }
    public int Hours { get; set; }
    public double TotalLines { get; set; }
    public double LinesPerHour { get; set; }
    public DateTime GoalStartDate { get; set; }
    public DateTime GoalEndDate { get; set; }

}

These are cast as IMyInterface, and returned in the list that I'm binding to the GridView.

Was it helpful?

Solution

Data bound controls do not use reflection but a TypeDescriptor to get the properties from a data source. In the TypeDescriptor.GetProperties method, you can read the following:

The properties for a component can differ from the properties of a class, because the site can add or remove properties if the component is sited.

Apparently the default implementation will only return direct properties from an Interface and not the inherited ones.

Luckily this mechanism is extensible, and you can write a TypeConverter class with custom property information implementation. Please refer to the remarks in the TypeConverter documentation for implementing property logic.

The GetProperties implementation of your custom TypeConverter class can call TypeDescriptor.GetProperties(Type) on your interface and all it's inherited interfaces. But maybe you could even write a generic TypeConverter that would find all inherited properties by using reflection.

Then you attach this custom TypeConverter to your interface with the TypeConverterAttribute attribute.

And then, like magic, the data source will find all properties. ;-)

OTHER TIPS

It's because an interface is a contract, and that's the only way to interact with an object is through that specific contract. The other interfaces cannot be assumed and can't be utilized until a cast is made.

So when you bind a List of T to something, the datagrid doesn't know about those other interfaces. And the datagrid isn't going to use reflection to figure out what other classes or interfaces might be inherited. The only object properties that are going to be available to the datagrid are the properties exposed by the T interface.

You need to bind List if you want the datagrid to have access to all the properties.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top