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

.NET WinUI 3报表预览打印实现及FastReport仅显示首条数据问题求助

.NET WinUI 3报表预览打印实现及FastReport仅显示首条数据问题求助

大家好,我目前在做一个.NET WinUI3桌面应用,卡在报表设计和打印这块了。我尝试在本地单独用FastReport Designer设计报表,然后把导出的.frx文件导入到项目里,但现在遇到个头疼的问题:用C#传递数据时,报表只显示第一条记录。

补充下背景:我在FastReport Designer里是用XML作为数据源来设计的,下面是我当前的实现代码:

private async void ImprimirRecibo(object obj)
{
    try
    {
        using (FastReport.Report reporte = new FastReport.Report())
        {
            string path = Path.Combine(AppContext.BaseDirectory, "Assets", "report1.frx");
            reporte.Load(path);

            int numeroRecibo = _recibo.idrecibos;
            reporte.SetParameterValue("nro_recibo", numeroRecibo);
            System.Diagnostics.Debug.WriteLine($"recibo: " + numeroRecibo);

            DataTable dt = await _reciboService.ListadoRecdAsync(numeroRecibo);
            dt.TableName = "Item";
            // Registrar DataTable con el mismo nombre que en el XSD
            reporte.RegisterData(dt, "Item");

            // Obtener el DataSource específico y asignarlo a la DataBand
            var dataSource = reporte.GetDataSource("Item");
            if (reporte.FindObject("Data1") is DataBand dataBand)
            {
                // Asignar el DataSource CORRECTO a la DataBand
                dataBand.DataSource = dataSource;
                System.Diagnostics.Debug.WriteLine($"DataSource asignado: {dataBand.DataSource}");
            }

            // Habilitar el datasource
            dataSource.Enabled = true;

            // Preparar reporte
            reporte.Prepare();

            using (MemoryStream ms = new MemoryStream())
            {
                var export = new PDFSimpleExport();
                reporte.Export(export, ms);
                ms.Position = 0;

                string tempFile = Path.Combine(Path.GetTempPath(), $"recibo_{numeroRecibo}.pdf");
                await File.WriteAllBytesAsync(tempFile, ms.ToArray());

                var ventanaPdf = new PdfViewerWindow(tempFile);
                ventanaPdf.Activate();
            }
        }
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine($"Error al imprimir: {ex.Message}");
    }
}

我排查过dt里的数据是完整的(不止一条记录),但报表就是只渲染第一条。有没有大佬能帮忙看看哪里出问题了?比如是不是FastReport Designer里的数据源配置和代码里的绑定没对应上?或者是DataBand的设置有什么遗漏?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.07 08:18:04