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

C#(.NET4)中Runtime自定义Stimulsoft报表StiTable为空问题求助

问题诊断与解决方案

核心问题分析

在自有.mrt报表中动态绑定StiTable时出现仅显示1列、内容为空的情况,主要原因包括:

  • 报表编译时机错误(先编译后注册数据,导致数据源未被正确识别)
  • 原有报表中StiTable的预设列结构未被清空,动态设置ColumnCount失效
  • CreateCell方法调用逻辑未适配已有表格结构
  • 数据单元格对齐属性赋值错误

分步修复方案

1. 调整数据注册与报表编译的顺序

将report.Compile()移到数据注册和字典同步之后,确保编译时已加载数据源:

private void PrintDataTable(string StiTableName, string DataSourceName, DataTable dataTable, StiReport report)
{
    DataView dataView = new DataView(dataTable);

    // 先设置脚本语言
    report.ScriptLanguage = StiReportLanguageType.CSharp;

    // 先注册数据
    report.RegData(DataSourceName, dataView);

    // 同步字典
    report.Dictionary.Synchronize();

    // 最后编译报表
    report.Compile();

    // 后续代码...
}

2. 清空原有表格的列结构

自有.mrt中的StiTable可能已存在预设列,需要先清空原有组件再重新创建单元格:

// 获取表格后先清空原有组件
StiTable table = (StiTable)report.GetComponentByName(StiTableName);
table.Components.Clear(); // 清空原有单元格

// 重新设置表格属性
table.DataSourceName = DataSourceName;
table.AutoWidthType = StiTableAutoWidthType.LastColumns;
table.ColumnCount = dataTable.Columns.Count;
table.RowCount = 3;
table.HeaderRowsCount = 1;
table.FooterRowsCount = 1;

// 重新创建单元格
table.CreateCell();

3. 修正数据单元格的索引计算与属性赋值

原代码中indexDataCell初始值计算错误,且误将对齐属性赋值给表头单元格,修正如下:

int indexHeaderCell = 0;
// 数据行起始索引 = 表头行数 × 列数
int indexDataCell = table.HeaderRowsCount * table.ColumnCount;

foreach (DataColumn column in dataView.Table.Columns)
{
    // 表头单元格设置不变...

    // 数据单元格设置
    StiTableCell dataCell = table.Components[indexDataCell] as StiTableCell;
    dataCell.HorAlignment = StiTextHorAlignment.Center;
    dataCell.VertAlignment = StiVertAlignment.Center; // 修正原代码赋值错误
    dataCell.Text.Value = $"{{{DataSourceName}.{Stimulsoft.Report.CodeDom.StiCodeDomSerializator.ReplaceSymbols(column.ColumnName)}}}";
    dataCell.Border = new StiBorder(StiBorderSides.All, Color.FromArgb(32, 178, 170), 1, StiPenStyle.Dash);
              
    indexHeaderCell++;
    indexDataCell++;
}

4. 添加数据源存在性验证

避免因数据源名称错误导致绑定失败:

// 同步字典后验证数据源是否存在
var dataSource = report.Dictionary.DataSources[DataSourceName];
if (dataSource == null)
{
    throw new InvalidOperationException($"数据源 {DataSourceName} 注册失败,请检查名称是否正确");
}

完整修复后的代码

private void PrintDataTable(string StiTableName, string DataSourceName, DataTable dataTable, StiReport report)
{
    DataView dataView = new DataView(dataTable);

    // 设置脚本语言
    report.ScriptLanguage = StiReportLanguageType.CSharp;

    // 注册数据到报表
    report.RegData(DataSourceName, dataView);

    // 同步字典,确保数据源被识别
    report.Dictionary.Synchronize();

    // 验证数据源是否存在
    var dataSource = report.Dictionary.DataSources[DataSourceName];
    if (dataSource == null)
    {
        throw new InvalidOperationException($"数据源 {DataSourceName} 注册失败,请检查名称是否正确");
    }

    // 编译报表(必须在数据注册后执行)
    report.Compile();

    // 获取报表中的表格组件
    StiTable table = (StiTable)report.GetComponentByName(StiTableName);
    if (table == null)
    {
        throw new InvalidOperationException($"未找到名称为 {StiTableName} 的StiTable组件");
    }

    // 清空原有表格组件,避免预设结构干扰
    table.Components.Clear();

    // 配置表格基础属性
    table.DataSourceName = DataSourceName;
    table.AutoWidthType = StiTableAutoWidthType.LastColumns;
    table.ColumnCount = dataTable.Columns.Count;
    table.RowCount = 3;
    table.HeaderRowsCount = 1;
    table.FooterRowsCount = 1;

    // 重新创建单元格结构
    table.CreateCell();

    // 设置表格样式
    table.TableStyleFX = new StiTable21StyleFX();
    table.TableStyle = Stimulsoft.Report.Components.Table.StiTableStyle.Style59;

    int indexHeaderCell = 0;
    // 计算数据行起始索引
    int indexDataCell = table.HeaderRowsCount * table.ColumnCount;

    foreach (DataColumn column in dataView.Table.Columns)
    {
        // 配置表头单元格
        StiTableCell headerCell = table.Components[indexHeaderCell] as StiTableCell;
        headerCell.Text.Value = column.Caption;
        headerCell.HorAlignment = StiTextHorAlignment.Center;
        headerCell.VertAlignment = StiVertAlignment.Center;

        // 配置数据单元格
        StiTableCell dataCell = table.Components[indexDataCell] as StiTableCell;
        dataCell.HorAlignment = StiTextHorAlignment.Center;
        dataCell.VertAlignment = StiVertAlignment.Center;
        dataCell.Text.Value = $"{{{DataSourceName}.{Stimulsoft.Report.CodeDom.StiCodeDomSerializator.ReplaceSymbols(column.ColumnName)}}}";
        dataCell.Border = new StiBorder(StiBorderSides.All, Color.FromArgb(32, 178, 170), 1, StiPenStyle.Dash);

        indexHeaderCell++;
        indexDataCell++;
    }

    // 配置页脚单元格
    StiTableCell footerCell = table.Components[table.Components.Count - 1] as StiTableCell;
    footerCell.Text.Value = "Count - {Count()}";
    footerCell.Font = new Font("Arial", 15, FontStyle.Bold);
    footerCell.VertAlignment = StiVertAlignment.Center;
    footerCell.HorAlignment = StiTextHorAlignment.Center;
}

额外检查项

  • 确认自有.mrt中的StiTable没有设置固定列数或锁定列属性
  • 检查报表的RenderingMode是否为StiRenderingMode.Design(设计模式可能影响动态绑定)
  • 确保数据列名称没有特殊字符,ReplaceSymbols方法已正确处理

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 18:40:32