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

如何在VB.NET(VS2022)中将非绑定数据传入DevExpress XtraReport1?

如何将非绑定窗体数据传入XtraReport1

方法一:自定义对象数据源(推荐,适配单条/多条数据)

这种方式适合结构化管理数据的场景,步骤清晰易维护:

  1. 创建数据模型类:根据窗体里的字段、变量定义对应的数据类,映射所有需要传递的内容:
public class ReportData
{
    public string CustomerName { get; set; }
    public decimal OrderTotal { get; set; }
    public DateTime OrderDate { get; set; }
    // 按需添加其他属性,对应窗体中的各类非绑定数据
}
  1. 收集窗体数据并实例化模型:在窗体中获取输入值,赋值给模型对象:
var reportData = new ReportData
{
    CustomerName = txtCustomer.Text,
    OrderTotal = decimal.Parse(txtTotal.Text),
    OrderDate = dtpOrderDate.Value
};
  1. 绑定到报表:将模型对象(多条数据就用List包装)设置为报表数据源,同时在设计器中绑定控件到模型属性:
var report = new XtraReport1();
// 单条数据用List包装,适配报表数据源的集合要求
report.DataSource = new List<ReportData> { reportData };
report.ShowPreview();

设计器绑定操作:打开XtraReport1设计器,添加ObjectDataSource组件,选择你创建的ReportData类,选中报表控件(如XRLabel),在属性面板的DataBindings中添加绑定,指定对应模型字段即可。

方法二:直接给报表控件赋值(适合单条简单数据)

如果数据量极少、结构简单,可跳过数据源,直接给报表控件手动赋值:

var report = new XtraReport1();
// 假设报表中有xrCustomer、xrTotal、xrDate三个XRLabel控件
report.xrCustomer.Text = txtCustomer.Text;
report.xrTotal.Text = $"合计:¥{txtTotal.Text}";
report.xrDate.Text = $"下单日期:{dtpOrderDate.Value:yyyy-MM-dd}";
report.ShowPreview();

补充说明

  • 若窗体中有批量数据(比如列表控件的多条记录),只需将多个ReportData对象存入List<ReportData>,再赋值给报表DataSource,报表会自动遍历生成对应条目。
  • 传递全局变量(如系统参数)时,可在XtraReport1类中添加公共属性,在窗体中直接赋值:
// 在XtraReport1类中定义属性
public string ReportTitle { get; set; }

// 窗体中赋值
var report = new XtraReport1();
report.ReportTitle = "客户订单报表";
// 报表设计器中将XRLabel的Text绑定到该属性即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 21:03:16