首页 新闻 搜索 专区 学院

DevExpress.XtraReports手动绑定数据只有一个值的问题

0
悬赏园豆:100 [已解决问题] 解决于 2010-09-08 22:21

1.XtraReports Class File:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;

/// <summary>
/// Summary description for XtraReport1
/// </summary>
public class XtraReport1 : DevExpress.XtraReports.UI.XtraReport
{
    private DevExpress.XtraReports.UI.DetailBand Detail;
    private DevExpress.XtraReports.UI.TopMarginBand TopMargin;
    private DevExpress.XtraReports.UI.BottomMarginBand BottomMargin;
    private XRLabel xrLabel1;
    private XRLabel xrLabel2;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    public XtraReport1()
    {
        InitializeComponent();
        //
        // TODO: Add constructor logic here
        //
    }
   
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing) {
        if (disposing && (components != null)) {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent() {
        string resourceFileName = "XtraReport1.resx";
        this.Detail = new DevExpress.XtraReports.UI.DetailBand();
        this.TopMargin = new DevExpress.XtraReports.UI.TopMarginBand();
        this.BottomMargin = new DevExpress.XtraReports.UI.BottomMarginBand();
        this.xrLabel1 = new DevExpress.XtraReports.UI.XRLabel();
        this.xrLabel2 = new DevExpress.XtraReports.UI.XRLabel();
        ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
        //
        // Detail
        //
        this.Detail.Controls.AddRange(new DevExpress.XtraReports.UI.XRControl[] {
            this.xrLabel1});
        this.Detail.HeightF = 100F;
        this.Detail.Name = "Detail";
        this.Detail.Padding = new DevExpress.XtraPrinting.PaddingInfo(0, 0, 0, 0, 100F);
        this.Detail.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopLeft;
        //
        // TopMargin
        //
        this.TopMargin.Controls.AddRange(new DevExpress.XtraReports.UI.XRControl[] {
            this.xrLabel2});
        this.TopMargin.HeightF = 100F;
        this.TopMargin.Name = "TopMargin";
        this.TopMargin.Padding = new DevExpress.XtraPrinting.PaddingInfo(0, 0, 0, 0, 100F);
        this.TopMargin.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopLeft;
        //
        // BottomMargin
        //
        this.BottomMargin.HeightF = 100F;
        this.BottomMargin.Name = "BottomMargin";
        this.BottomMargin.Padding = new DevExpress.XtraPrinting.PaddingInfo(0, 0, 0, 0, 100F);
        this.BottomMargin.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopLeft;
        //
        // xrLabel1
        //
        this.xrLabel1.LocationFloat = new DevExpress.Utils.PointFloat(146.875F, 20.79166F);
        this.xrLabel1.Name = "xrLabel1";
        this.xrLabel1.Padding = new DevExpress.XtraPrinting.PaddingInfo(2, 2, 0, 0, 96F);
        this.xrLabel1.SizeF = new System.Drawing.SizeF(100F, 23F);
        this.xrLabel1.Text = "xrLabel1";
        //
        // xrLabel2
        //
        this.xrLabel2.Font = new System.Drawing.Font("Times New Roman", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.xrLabel2.LocationFloat = new DevExpress.Utils.PointFloat(25.00002F, 10.00001F);
        this.xrLabel2.Name = "xrLabel2";
        this.xrLabel2.Padding = new DevExpress.XtraPrinting.PaddingInfo(2, 2, 0, 0, 96F);
        this.xrLabel2.SizeF = new System.Drawing.SizeF(615F, 68.16668F);
        this.xrLabel2.StylePriority.UseFont = false;
        this.xrLabel2.StylePriority.UseTextAlignment = false;
        this.xrLabel2.Text = "test";
        this.xrLabel2.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
        //
        // XtraReport1
        //
        this.Bands.AddRange(new DevExpress.XtraReports.UI.Band[] {
            this.Detail,
            this.TopMargin,
            this.BottomMargin});
        this.Version = "9.3";
        ((System.ComponentModel.ISupportInitialize)(this)).EndInit();

    }

    #endregion
}

2.page File:

using System;
using System.Collections.Generic;
using DevExpress.XtraReports.UI;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        XtraReport1 rpt = new XtraReport1();
        this.ReportViewer1.Report = rpt;
        XRLabel lbl = (XRLabel)rpt.FindControl("xrLabel2", true);

        List<Student> stulst = new List<Student>();
        for (int i = 0; i < 50; i++)
        {
            Student stu = new Student();
            stu.Name = "name" + i;
            stulst.Add(stu);
        }
        rpt.Bands[BandKind.Detail].Controls.Add(lbl);
        lbl.DataBindings.Add("Text", stulst, "Name");
       

    }
    class Student
    {
        string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        string sex;

        public string Sex
        {
            get { return sex; }
            set { sex = value; }
        }
    }
}

3.结果是这个xrLabel2在页面上只有一个值name0,而期望的结果应该是50条记录,请高手指点,多谢!

4.手动绑定时,不同的控件如何绑定不同的数据源?(因为绑定时要report.datasource=list)

问题补充: 4.手动绑定时,多个控件(如lable)如何绑定不同的list(数据源)
庹林的主页 庹林 | 初学一级 | 园豆:0
提问于:2010-08-13 10:18
< >
分享
最佳答案
0

把你的  cs  文件的代码整理了下:

在  Page_Load 事件里面调用

  protected void Page_Load(object sender, EventArgs e)
        {
            ReportViewer1.Report = CreateReport();
        }

    //独立出一个创建报表的方法

     XtraReport CreateReport()
        {
            List<Student> stulst = new List<Student>();
            for (int i = 0; i < 8; i++)
            {
                Student stu = new Student();
                stu.Name = "name" + i;
                stu.Sex = "男";
                stulst.Add(stu);
            }

            XtraReport1 report = new XtraReport1();

            report.DataSource = stulst;
            report.BindDetailLabel("xrLabel1", "Name");
            report.BindDetailLabel("xrLabel2", "Sex");

            return report;
        }

报表 XtraReport1 文件 公开个方法来绑定数据源到 报表 Detials 节 的XrLabel 控件上

     /// <summary>
        /// 绑定数据源
        /// </summary>
        /// <param name="labelName">xrLabel 控件名</param>
        /// <param name="boundName">绑定的数据源字段</param>
        public void BindDetailLabel(string labelName, string boundName)
        {
            var lbl = Detail.FindControl(labelName, true) as XRLabel;

            if (lbl != null)
            {
                lbl.DataBindings.Add("Text", DataSource, boundName);
            }
        }
       这样就可以显示了。

至于 问题补充:4.手动绑定时,多个控件(如lable)如何绑定不同的list(数据源)“这个问题。如果要在一个报表里面显示不同数据源,这个应该做不来, 因为一张报表只能有一个数据源、除非你使用主从报表(子报表)。

下面是两文件完整的代码:

_Default.aspx.cs:


    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ReportViewer1.Report = CreateReport();
        }

        XtraReport CreateReport()
        {
            List<Student> stulst = new List<Student>();
            for (int i = 0; i < 50; i++)
            {
                Student stu = new Student();
                stu.Name = "name" + i;
                stu.Sex = "男";
                stulst.Add(stu);
            }

            XtraReport1 report = new XtraReport1();

            report.DataSource = stulst;
            report.BindDetailLabel("xrLabel1", "Name");
            report.BindDetailLabel("xrLabel2", "Sex");

            return report;
        }
    }

    class Student
    {
        string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        string sex;

        public string Sex
        {
            get { return sex; }
            set { sex = value; }
        }
    }

 

XtraReport1.cs:


    public partial class XtraReport1 : DevExpress.XtraReports.UI.XtraReport
    {
        public XtraReport1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 绑定数据源
        /// </summary>
        /// <param name="labelName">xrLabel 控件名</param>
        /// <param name="boundName">绑定的数据源字段</param>
        public void BindDetailLabel(string labelName, string boundName)
        {
            var lbl = Detail.FindControl(labelName, true) as XRLabel;

            if (lbl != null)
            {
                lbl.DataBindings.Add("Text", DataSource, boundName);
            }
        }
       
    }

收获园豆:100
HUHU慈悲 | 大侠五级 |园豆:9973 | 2010-08-14 00:17
多谢!辛苦了!
庹林 | 园豆:0 (初学一级) | 2010-08-18 09:32
其他回答(2)
0

有个自动生成列的属性,我用过winform的版本。

Launcher | 园豆:45045 (高人七级) | 2010-08-13 10:56
0

仍然只显示一条记录啊

啸尘 | 园豆:202 (菜鸟二级) | 2013-03-20 21:44

加一句代码:

DataSource = dt;(dt为DataTable,数据源)

支持(0) 反对(0) 神言 | 园豆:200 (初学一级) | 2016-11-18 09:45
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册