PerformDataBinding, extract row count from ObjectDataSource

649 Views Asked by At

I have a custom GridView which automatically puts the row count from SqlDataSources into grids for me. It computes that count in the code below. Note that this question relates to the custom inherited GridView control, not page-level stuff.

How do I recognise in PerformDataBinding that the "IEnumerable" thing is an ObjectDataSource? I want to find out specifically what ObjectDataSource type it is, then call its "get total row count" function.

The reason is that the total row count is (say) millions, where as at the moment the ICollection clause returns the count of just what has been retrieved from the database, which is typically "one page" of data, so (say) 20 records not 20,000,000!

I only have a couple of specific ObjectDataSource types, so I could pick them out one by one if I knew how to find their names from this IEnumerable thing.

I have reviewed this answer: How to get row count of ObjectDataSource but I don't know how to work out which precise BLL I'm dealing with. The debugger has lots of stuff inside this object, but I can't see what I want there.

protected override void PerformDataBinding(IEnumerable data)
{
   // This does not work for my Object Data Sources, which return one page of 
   // records only, not the whole set. There must however be a way...
   if (data is IListSource)
   {
      IListSource list = (IListSource)data;
      rowcount = list.GetList().Count;
   }
   else if (data is ICollection)
   {
      ICollection collection = (ICollection)data;
      rowcount = collection.Count;    
   }
   base.PerformDataBinding(data);
}
1

There are 1 best solutions below

1
On

Just enumerate without casting.

protected override void PerformDataBinding(IEnumerable data)
        {
            var enum1 = data.GetEnumerator();
            int count = 0;
            while (enum1.MoveNext())
            {
                count++;
            }
            this.TotalRecordCount = count;

            base.PerformDataBinding(data);
        }