You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#中实现Crystal Report两列分范围显示记录编号

在Crystal Report中通过C#实现分列显示指定范围记录编号

方法一:构造配对数据源绑定(推荐)

这种方式通过C#提前整理好符合两列布局的数据,直接绑定到报表,逻辑清晰易维护。

  1. 定义报表数据实体
    创建一个用于承载左右两列数据的类:
public class ReportItem
{
    public int LeftRecordId { get; set; }
    public int RightRecordId { get; set; }
}
  1. 生成符合要求的数据源
    拆分1-5和6-12的编号,组合成适配两列的数据集:
List<ReportItem> reportData = new List<ReportItem>();

// 先处理1-5和6-10的配对
for (int i = 0; i < 5; i++)
{
    reportData.Add(new ReportItem
    {
        LeftRecordId = i + 1,
        RightRecordId = i + 6
    });
}

// 添加剩余的11、12号记录(仅第二列显示)
reportData.Add(new ReportItem { LeftRecordId = 0, RightRecordId = 11 });
reportData.Add(new ReportItem { LeftRecordId = 0, RightRecordId = 12 });
  1. 绑定数据源到Crystal Report
    将生成的数据集绑定到报表并显示:
ReportDocument crReport = new ReportDocument();
// 加载你的报表文件
crReport.Load(@"C:\YourReportPath\YourReport.rpt");
// 设置数据源
crReport.SetDataSource(reportData);
// 绑定到报表查看器
crystalReportViewer1.ReportSource = crReport;
crystalReportViewer1.Refresh();
  1. 报表布局调整
    在Crystal Report设计器中:
  • 添加两个文本框分别绑定LeftRecordId和RightRecordId
  • 为LeftRecordId字段设置抑制显示:右键字段 → 格式字段 → 公共 → 勾选「抑制显示」,输入公式{ReportItem.LeftRecordId}=0,这样空的左列会自动隐藏

方法二:利用报表公式直接筛选

如果你的数据源是包含1-12编号的单列表,可通过Crystal Report的公式字段实现分列显示:

  1. 绑定单列数据源
    在C#中将包含1-12编号的集合(如List<int>或带Id字段的实体类)绑定到报表。

  2. 创建公式字段

  • 第一列公式(仅显示1-5):
If {YourDataSource.Id} >= 1 And {YourDataSource.Id} <=5 Then ToText({YourDataSource.Id}) Else ""
  • 第二列公式(仅显示6-12):
If {YourDataSource.Id} >=6 And {YourDataSource.Id} <=12 Then ToText({YourDataSource.Id}) Else ""
  1. 调整报表布局
    将两个公式字段分别放在左右两列,设置报表细节节高度适配单行显示,即可实现对应范围的编号分列展示。

内容的提问来源于stack exchange,提问作者Met Mok

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 21:23:17