PerformDataBinding,从ObjectDataSource中提取行数

时间:2011-10-13 09:38:49

标签: c# asp.net gridview objectdatasource rowcount

我有一个自定义GridView,它会自动将来自SqlDataSources的行数放入网格中。它计算下面代码中的计数。请注意,此问题与自定义继承的GridView控件有关,而与页面级别的内容无关。

如何在PerformDataBinding中识别出“IEnumerable”是ObjectDataSource?我想具体找出它是什么ObjectDataSource类型,然后调用它的“get total row count”函数。

原因是总行数是(比方说)数百万,其中ICollection子句返回从数据库中检索到的数据,这通常是“一页”数据,因此(说)20条记录不是20,000,000!

我只有几个特定的​​ObjectDataSource类型,所以如果我知道如何从这个IEnumerable东西中找到它们的名字,我可以逐个挑选它们。

我已经回复了这个答案: How to get row count of ObjectDataSource 但我不知道如何找出我正在处理的精确BLL。调试器在这个对象中有很多东西,但我看不到我想要的东西。

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 个答案:

答案 0 :(得分:0)

只需枚举而不进行投射。

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

            base.PerformDataBinding(data);
        }