【问题标题】:bind an object model to XtraReport devexpress将对象模型绑定到 XtraReport devexpress
【发布时间】:2013-08-25 17:03:59
【问题描述】:

我需要将 xtrareport (devexpress) 绑定到对象模型。

假设我的模型是:

public class ReportViewModel
{
        public Header Header { get; set; }
        public Body Body { get; set; }
        public Footer Footer { get; set; }
}

我已经通过设计器完成了模板报告。

我应该如何使用 C# 从视图模型中提供报告?

这个不行

XtraReport1 report = new XtraReport1();
report.DataSource = viewModel;

提前致谢。

【问题讨论】:

    标签: c# visual-studio-2010 devexpress reporting


    【解决方案1】:

    仅将报表的 DataSource 设置为您的 ViewModel 是不够的,您还需要将控件绑定到相应的字段。以下是我在 WinForms 中为报表所做的类似操作:

    public IssueReport(DataTable issuesTable)
    {
        InitializeComponent();
    
        this.DataSource = issuesTable;
    
        xrlabelIssueNumber.DataBindings.Add("Text", this.DataSource, "IssueID");
        xrlabelAssignedUser.DataBindings.Add("Text", this.DataSource, "Assigned User");
        xrlabelPriority.DataBindings.Add("Text", this.DataSource, "Priority");
        xrlabelCategory.DataBindings.Add("Text", this.DataSource, "IssueCategory");
        xrlabelReceivedDate.DataBindings.Add("Text", this.DataSource, "ReceivedDate");
        xrlabelDueDate.DataBindings.Add("Text", this.DataSource, "DueDate");
        xrlabelProduct.DataBindings.Add("Text", this.DataSource, "Product");
        xrlabelStatus.DataBindings.Add("Text", this.DataSource, "Status");
        xrlabelSubStatus.DataBindings.Add("Text", this.DataSource, "Sub-Status");
        xrlabelVersion.DataBindings.Add("Text", this.DataSource, "VersionNumber");
        xrlabelCustomer.DataBindings.Add("Text", this.DataSource, "CustomerName");
        xrlabelLocation.DataBindings.Add("Text", this.DataSource, "LocationName");
        xrlabelRoom.DataBindings.Add("Text", this.DataSource, "RoomName");
        xrlabelPOC.DataBindings.Add("Text", this.DataSource, "POC");
        xrlabelOfficeNumber.DataBindings.Add("Text", this.DataSource, "OfficePhone");
        xrlabelCallbackNumber.DataBindings.Add("Text", this.DataSource, "CallbackNumber");
        xrlabelEmail.DataBindings.Add("Text", this.DataSource, "Email");
        xrlabelAlternateEmail.DataBindings.Add("Text", this.DataSource, "AlternateEmail");
        xrlabelSummary.DataBindings.Add("Text", this.DataSource, "IssueSummary");
    

    }

    DataBindings.Add 方法接受 3 个参数;第一个是要绑定到的对象的属性(99% 的时间是 XtraReportLabel 的 Text 属性)。第二个是 BindingSource (在您的情况下,您的 ViewModel ......但这可能需要先转换为某种类型的 BindingList)。第三个是您要使用的 BindingSource 的字段。

    希望对您有所帮助....

    【讨论】:

    • 另外,要绑定一个对象,它必须是一个列表 并且必须将一个绑定源(windows 窗体控件)放到报告documentation.devexpress.com/#xtrareports/CustomDocument7547
    • 如果要执行该绑定,则选择标签可以在设计时选择绑定字段或子项目(请参阅DOC) span>
    【解决方案2】:

    你必须发送列表。

    var viewModelList = new List<ReportViewModel>(){viewModel};
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-29
      • 2013-04-21
      • 1970-01-01
      • 1970-01-01
      • 2019-02-13
      • 2015-05-08
      • 1970-01-01
      相关资源
      最近更新 更多