ReportViewer 2010 struggling with polymorphism

此生再无相见时 提交于 2019-12-11 07:35:10

问题


I have a rdlc report that takes as a ReportDataSource a List<BaseClass>. BaseClass has two derived classes A and B.
In the report, I group based on a property of the base class. As long as the list only contains objects of A or B, all works fine. However if I mix instances from A and B, then the report creation fails with the following message:

The Group expression used in grouping '[Group Name]' references a dataset field which contains an Error: FieldValueException

The property returns for both classes a simple string literal, backed by a constant of the classes, there is nothing that could be wrong with this. I also checked all other used properties, but there is nothing wrong with them.
Has anybody else seen this behaviour or has someone an explantion for this behaviour? It seems to me that report viewer don't likes polymorphism! Could that be?

Example

public abstract class BaseClass{
   public abstract string GroupKey{get;}
}
public class A : BaseClass{
    public override string GroupKey{
       get{
          return ...
       }
    }
}
public class B : BaseClass{
    public override string GroupKey{
       get{
          return ...
       }
    }
}

回答1:


It turned out that this is another limitation of Report Viewer. As a solution I have created a class C that derives also from BaseClass and wrapps an instance of BaseClass.
Before providing my List<BaseClass> as a DataSource for Report Viewer, I wrap all contained instances of A and B with an instance of C and give then the list of C to Report Viewer. So all instances are of the same type and Report Viewer is happy.

Here an example. I hope this helps someone in the same situation:

public abstract class BaseClass{
   public string GroupKey{get;}
   public virtual C GetWorkaroundWrapper(){
       return new C(this);
   }
}
public class A : BaseClass{
    public override string GroupKey{
       get{
          return ...
          }
    }
}
public class B : BaseClass{
    public override string GroupKey{
       get{
          return ...
          }
    }
}
public class C : BaseClass{
    BaseClass m_baseClass;
    public C(BaseClass baseClass){
         if(null == baseClass){
             throw new ArgumentNullException("baseClass");
         }
         m_baseClass=baseClass;
    }
    public override string GroupKey{
         get{
             return m_baseCLass.GroupKey;
         }
    }
    public override C GetWorkaroundWrapper(){
         return this;
    }
}

The GetWorkaroundWrapper-Methodis only for convenience. With this, the creation of the wrapper is simplified:

List<C> workaroundList=new List<C>();
foreach(BaseClass item in sourceList){
 workaroundList.Add(item.GetWorkaroundWrapper());
}

dataSource.Value=workaroundList;

Please note that it is not important that the list is of C. It works also with a list of BaseClass, but its more clean to use a list of C.



来源:https://stackoverflow.com/questions/5492156/reportviewer-2010-struggling-with-polymorphism

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!