Skip to main content
 首页 » 编程设计

c#之Crystal 报表 : Place row data from grouped records into columns

2024年02月23日15zengkefu

好的,所以我的 Crystal Reports 数据源有如下所示的行:

|--------+----------+---------+------------+-----------+-----------+---------| 
| SiteNo | SiteName | SiteMgr | ContType   | ContName  | ContState | ContZip | 
|--------+----------+---------+------------+-----------+-----------+---------| 
| 1262   | S. Belt  | Joe B.  | Landlord   | Mike      | CA        | 90017   | 
| 1262   | S. Belt  | Joe B.  | Architect  | Paul      | TX        | 77040   | 
| 1262   | S. Belt  | Joe B.  | Contractor | Chris     | AZ        | 85016   | 
|--------+----------+---------+------------+-----------+-----------+---------| 

有数百个站点编号(SiteNo),每个站点编号有三行......报告中的每条记录需要这样格式化:

|------------+------------+------------+------------| 
|    Site    |  Landlord  | Architect  | Contractor | 
|------------+------------+------------+------------| 
| 1262       | Mike       | Paul       | Chris      | 
| S. Belt    | CA         | TX         | AZ         | 
| Joe B.     | 90017      | 77040      | 85016      | 
|------------+------------+------------+------------| 

由于数据源的前三列(SiteNo、SiteName、SiteMgr)对于特定站点始终相同,因此报告按 SiteNo 分组。我已经弄清楚了这部分。我把它放在组页脚中。然后,我遇到的问题是,根据联系人类型(ContType:房东、建筑师或承包商),信息需要放在相关的列中。

不确定执行此操作的最佳方法?任何帮助将不胜感激。

请您参考如下方法:

您可以通过使用 Crystal 公式来实现这一点,但我认为如果您像这样修改 SQL 查询,它会更容易理解和维护:

select SiteNo, 
       max(SiteName)     SiteName, 
       max(SiteMgr)      SiteMgr, 
       max(case ContType 
               when 'Landlord' then ContName 
               else NULL 
           end)          LandlordContName, 
       max(case ContType 
               when 'Landlord' then ContState 
               else NULL 
           end)          LandlordContState, 
       max(case ContType 
               when 'Landlord' then ContZip 
               else NULL 
           end)          LandlordContZip, 
       max(case ContType 
               when 'Architect' then ContName 
               else NULL 
           end)          ArchitectContName, 
       max(case ContType 
               when 'Architect' then ContState 
               else NULL 
           end)          ArchitectContState, 
       max(case ContType 
               when 'Architect' then ContZip 
               else NULL 
           end)          ArchitectContZip, 
       max(case ContType 
               when 'Contractor' then ContName 
               else NULL 
           end)          ContractorContName, 
       max(case ContType 
               when 'Contractor' then ContState 
               else NULL 
           end)          ContractorContState, 
       max(case ContType 
               when 'Contractor' then ContZip 
               else NULL 
           end)          ContractorContZip 
from Contacts 
group by SiteNo 

加长报告的详细信息部分,使其可以包含三行,然后放置:

  • SiteNo、LandlordContName、ArchitectContName 和 ContractorContName 在第一行;
  • 第二行的 SiteName、LandlordContState、ArchitectContState 和 ContractorContState;
  • 第三行的 SiteMgr、LandlordContZip、ArchitectContZip 和 ContractorContZip。