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.

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


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


5 thoughts on “MoreDefensiveDatasource

  • Vexed Student says:

    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.

  • Gunnar says:

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

  • Aredhel says:

    for anyone coming here later, the codes can be downloaded here:

    code.google.com/…/defensivedatasource

  • Gunnar says:

    Thank you, Aredhel! 🙂

  • Frederic says:

    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 *