At the end of the previous month I wrote a posting on DefensiveDatasource class that can be used to associate ASP.NET’s GridView with collections containing objects of different types and based on one and the same base class. Dividing data into pages in GridView does, however, not succeed. An error message "The data source does not support server-side data paging" is displayed.

There is a simple solution to this problem. We have to create a new class conforming to the ICollection interface based on DefensiveDatasource. As we need to deal with DefensiveDatasource only in case of collections of the IList type, I restricted the collection selection of the new class so that only IList based collections can be submitted to the class as initial collections.

public class MoreDefensiveDatasource : DefensiveDatasource, ICollection
    private IList list;

    public MoreDefensiveDatasource(IList innerSource, IDictionary propertyFilter)
        : base((IEnumerable)innerSource, propertyFilter)
        this.list = innerSource;
    int ICollection.Count
        get { return list.Count; }

    void ICollection.CopyTo(Array array, int index)
        this.list.CopyTo(array, index);
    bool ICollection.IsSynchronized
        get { return this.list.IsSynchronized; }
    object ICollection.SyncRoot
        get { return this.list.SyncRoot; }

For more extensive collection support I recommend using the ICollection interface as a restriction.

Liked this post? Empower your friends by sharing it!
Categories: .NET

View Comments (5)

  • Hi. I'm experiencing a problem like this on an ASP.NET project I'm currently doing. I have an arraylist of interface types, containing two different classes implementing that interface. When databinding to the gridview, it fails, of course.

    Sean Foy's site (sean-janus.optionpc.com/.../DefensiveDatasource ) is down. Do you have this class available anywhere else?


  • Can you send me your e-mail address? I have binaries of this class in my source code repository.

  • This solution has a drawback : if your grid use template fields, with custom binding relying on manipulating your base class, you will get an invalid cast exception.

    The defensivedatasource made your grid bound to an object type which is not derivate from your original base type in your list.

    Personally, I have ended up reimplementing a custom hyperlinkfield polymorphism aware. And I used it instead of the vanilla hyperlinkfield.

    This suppose to supply custom field for any vanilla field that does not support polymorphism (I do not know a list of such fields).

Related Post