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

RDLC报表Tablix无法从数据集加载数据问题求助

RDLC报表Tablix无法加载数据集值的排查方案

问题背景

开发基于Microsoft RDLC的发票打印应用,报表可正常显示参数类数据(如日期、客户名),但Tablix组件无法加载从数据集传入的商品明细数据,已尝试YouTube上的各类解决方案均无效。

相关代码

private void InvoiceButton_Click(object sender, EventArgs e)
{
    Parameters p = new Parameters();
    List<Parameters> lst = new List<Parameters>();
    p.date = CurrentDateTimePicker.Text;
    p.CustomerName = CustomerTextBox.Text;

    if (CartDataGridView.Rows.Count != 0)
    {
        for (int i = 0; i < CartDataGridView.Rows.Count-1; i++)
        {
            lst.Add(new Parameters
            {
                ItemName = CartDataGridView.Rows[i].Cells[1].Value.ToString(),
                Price = CartDataGridView.Rows[i].Cells[2].Value.ToString(),
                Quantity = CartDataGridView.Rows[i].Cells[3].Value.ToString(),
                Company = CartDataGridView.Rows[i].Cells[4].Value.ToString(),
                ExpiryDate = CartDataGridView.Rows[i].Cells[5].Value.ToString(),
                Total = CartDataGridView.Rows[i].Cells[7].Value.ToString()
                // Subtotal = (Convert.ToInt32(CartDataGridView.Rows[i].Cells[7].Value))
            });
        }
    }

    InvoiceForm f = new InvoiceForm();
    ReportDataSource rd = new ReportDataSource("MyDataSet");
    
    rd.Value = lst;
    f.reportViewer1.LocalReport.ReportEmbeddedResource = "CPMSTestPhase.InvoiceReport.rdlc";
    f.reportViewer1.LocalReport.DataSources.Add(rd);

    ReportParameter[] rptparam = new ReportParameter[]
    {
        new ReportParameter("Date",p.date),
        new ReportParameter("CustomerName",p.CustomerName),
        // new ReportParameter("Subtotal",p.Subtotal.ToString()),
    };
    f.reportViewer1.LocalReport.SetParameters(rptparam);

    f.reportViewer1.RefreshReport();
    f.ShowDialog();
}

报表异常表现

报表Tablix区域无数据显示截图

排查与解决步骤

  • 检查数据集名称匹配:确认RDLC报表中定义的数据集名称与代码中ReportDataSource("MyDataSet")的名称完全一致(大小写敏感),包括空格、拼写。
  • 验证实体类属性映射:确保Parameters类的属性名称(如ItemName、Price)与RDLC报表Tablix绑定的字段名称完全匹配,无大小写或拼写差异。
  • 确认数据集数据不为空:在代码中添加调试代码,检查lst集合是否正确获取了CartDataGridView中的数据,比如在rd.Value = lst;前打印lst.Count,确认有数据。
  • 检查报表资源路径:验证ReportEmbeddedResource的路径"CPMSTestPhase.InvoiceReport.rdlc"是否正确,确保报表文件的“生成操作”设置为“嵌入的资源”。
  • 修正循环边界错误:代码中循环条件i < CartDataGridView.Rows.Count-1会漏掉最后一行数据,改为i < CartDataGridView.Rows.Count(若DataGridView无自动添加的空行),或判断行是否为新行再跳过。
  • 调整报表刷新时机:将f.reportViewer1.RefreshReport();移到f.ShowDialog();之后,或先调用f.reportViewer1.LocalReport.Refresh();再执行刷新操作。
  • 检查Tablix数据源绑定:打开RDLC报表设计器,确认Tablix的“数据源”已选择对应的数据集,且每个单元格绑定的字段正确。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 02:16:14