MoreDefensiveDatasource

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.

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    5 thoughts on “MoreDefensiveDatasource

    • June 1, 2008 at 3:18 am
      Permalink

      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?

      Cheers.

    • June 1, 2008 at 9:40 am
      Permalink

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

    • December 1, 2010 at 5:11 pm
      Permalink

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

    Leave a Reply

    Your email address will not be published. Required fields are marked *