【问题标题】:How to set datasource in report viewer at runtime in devexpress winform application?如何在 devexpress winform 应用程序运行时在报表查看器中设置数据源?
【发布时间】:2017-10-22 22:11:22
【问题描述】:

我正在尝试在 Devexpress winform 中显示报告。以下是我迄今为止尝试过的。

string test = @"C:\Users\bajay\Desktop\First.repx";

if (File.Exists(test))
     MessageBox.Show("Test");
     
XtraReport report = new XtraReport();
report.LoadLayout(test);
var tool = new ReportPrintTool(report);
    tool.ShowPreview();

【问题讨论】:

    标签: c# winforms devexpress xtrareport


    【解决方案1】:

    不,你错了,因为你只是加载了实际上没有数据库的 .REPX 文件。它只有布局,你应该连接 SQL、JSONDataSource、WCF 等数据库。通过 SQLDataSource 组件,你应该在设计时连接一次,然后做你想做的代码,在运行时之后只需更改连接字符串,但请确保并记住在连接字符串中添加 XPO。

    以更好的方式理解。 1. 在设计时连接到数据库 2. 惩罚你的项目 3. 简单地更改客户端机器上的连接字符串 4.你会完成的!

    【讨论】:

      【解决方案2】:

      您必须使用报表的 DataSource 属性提供数据。 这可以是从您的数据库中填充的 DataTable,也可以是 IEnumrable/对象列表。

      report.DataSource = YourItemFactory.GetYourItems();
      

      关于这个话题你可以参考DX-support pages

      【讨论】:

        【解决方案3】:

        我建议您阅读 XtraReport 文档的Provide Data to a Report

        在那里,您将找到文档列出了设计时教程和运行时示例,这些示例说明了如何将报表及其元素(例如计算字段和参数)连接到 various kinds of data sources

        示例:

        using System;
        using System.Windows.Forms;
        using System.Collections.Generic;
        using DevExpress.XtraReports.UI;
        // ... 
        
        private void Form1_Load(object sender, EventArgs e) {
            XtraReport1 report = new XtraReport1();
            report.DataSource = CreateData();
        
            ReportPrintTool tool = new ReportPrintTool(report);
            tool.ShowPreview();
        }
        
        private  List<Data> CreateData() {
            List<Data> data = new List<Data>();
        
            Data item1 = new Data();
            item1.Date = DateTime.Now;
            item1.Id = 0;
            item1.Name = "First";
            data.Add(item1);
        
            Data item2 = new Data();
            item2.Date = DateTime.Now;
            item2.Id = 1;
            item2.Name = "Second";
            data.Add(item2);
        
            Data item3 = new Data();
            item3.Date = DateTime.Now;
            item3.Id = 2;
            item3.Name = "Third";
            data.Add(item3);
        
            return data;
        }
        

        参考资料:
        How to: Bind a Report to a Data Source Schema
        How to: Bind a Report to a List Object at Design Time and Provide Data at Runtime

        How to: Bind a Report to an Array List
        How to: Bind a Report to a Collection that Implements the ITypedList Interface
        How to: Bind a Report to an XML File (Runtime Sample)

        希望对您有所帮助..

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-24
          相关资源
          最近更新 更多