0

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);
}
Community
  • 1
  • 1
philw
  • 661
  • 10
  • 20
  • Eventually I worked out a way around this: Override CreateChildControls, which has inside it an int which is the total row count in the data source. That's the number you need, and it's right there for the taking. – philw Jan 28 '14 at 12:40

1 Answers1

0

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);
        }
lauh
  • 22
  • 2
  • That may take quite a long time; it seems a shame to load all the records and then count them one by one, when the basic point of what I'm doing is to avoid loading those records, if you see what I mean. – philw Jan 28 '14 at 12:07