我需要将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;提前谢谢。
发布于 2013-08-28 06:27:32
仅仅将报告的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...but可能需要先转换为某种BindingList )。第三个是您要使用的BindingSource的字段。
希望这能帮上忙。
发布于 2021-04-04 20:57:15
你必须发送列表。
var viewModelList = new List<ReportViewModel>(){viewModel};https://stackoverflow.com/questions/18431502
复制相似问题