dev report绑定DataTable

效果图
在这里插入图片描述
图片与案例非同一程序,

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using System.Collections.Generic;
using MvcApplication1.Models;
using System.Data;

/// <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;
    protected int XRTableCellHeight = 38;
    private ReportHeaderBand ReportHeader;
    private XRLabel xrLabel2;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    public XtraReport1()
    {
        InitializeComponent();
        XtraReport rpt = this;// 建立报表实例
        rpt.DataSource = FillDataset();//设置报表数据源  
        InitBands(rpt);//添加带区(Bands) 
        InitDetailsBasedonXRTable(rpt);//用XRTable显示报表 
    }
    public DataSet FillDataset()
    {
        DataSet myDataSet = new DataSet();
        myDataSet.DataSetName = "myDataSet";
        DataTable table = new DataTable("Detail");

        myDataSet.Tables.Add(table);

        table.Columns.Add("Name", typeof(String));
        table.Columns.Add("Address", typeof(String));
        table.Columns.Add("Sex", typeof(String));
        table.Columns.Add("Birthplace", typeof(String));
        table.Columns.Add("Birthday", typeof(String));

        for (int i = 0; i < 50; i++)
        {
            table.Rows.Add(new object[] { i, "1", "2", "3", "4" });
            table.Rows.Add(new object[] { i, "11", "22", "33", "44" });
       
        }

        return myDataSet;
    }
    public void InitBands(XtraReport rpt)
    {
        DetailBand detail = new DetailBand();
        PageHeaderBand pageHeader = new PageHeaderBand();
        ReportFooterBand reportFooter = new ReportFooterBand();
        detail.Height = XRTableCellHeight;
        reportFooter.Height = 380;
        pageHeader.Height = XRTableCellHeight;

        rpt.Bands.AddRange(new DevExpress.XtraReports.UI.Band[] { detail, pageHeader, reportFooter });
    }
    public void InitDetailsBasedonXRTable(XtraReport rpt)
    {
        DataSet ds = ((DataSet)rpt.DataSource);
        int colCount = ds.Tables[0].Columns.Count;
        int colWidth = (rpt.PageWidth - (rpt.Margins.Left + rpt.Margins.Right)) / colCount;

        // Create a table to represent headers
        XRTable tableHeader = new XRTable();
        tableHeader.Height = XRTableCellHeight;
        tableHeader.Width = (rpt.PageWidth - (rpt.Margins.Left + rpt.Margins.Right));
        tableHeader.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
        tableHeader.Font = new System.Drawing.Font("宋体", 12.75F, System.Drawing.FontStyle.Bold);

        // Create a table to display data
        XRTable tableDetail = new XRTable();
        tableDetail.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
        tableDetail.Width = (rpt.PageWidth - (rpt.Margins.Left + rpt.Margins.Right));
        tableDetail.Height = XRTableCellHeight;
        tableDetail.Font = new System.Drawing.Font("宋体", 12.75F, System.Drawing.FontStyle.Regular);


        XRTableRow headerRow = new XRTableRow();
        XRTableRow detailRow = new XRTableRow();
        // Create table cells, fill the header cells with text, bind the cells to data
        for (int i = 0; i < colCount; i++)
        {
            XRTableCell headerCell = new XRTableCell();
            headerCell.Width = colWidth;
            headerCell.Borders = DevExpress.XtraPrinting.BorderSide.All;
            headerCell.Text = ds.Tables[0].Columns[i].Caption;

            XRTableCell detailCell = new XRTableCell();
            detailCell.Width = colWidth;
            detailCell.DataBindings.Add("Text", null, ds.Tables[0].Columns[i].Caption);
            detailCell.Borders = DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Right | DevExpress.XtraPrinting.BorderSide.Bottom;

            // Place the cells into the corresponding tables
            headerRow.Cells.Add(headerCell);
            detailRow.Cells.Add(detailCell);
        }

        headerRow.Width = tableHeader.Width;
        tableHeader.Rows.Add(headerRow);

        detailRow.Width = tableDetail.Width;
        tableDetail.Rows.Add(detailRow);

        // Place the table onto a report's Detail band
        rpt.Bands[BandKind.PageHeader].Controls.Add(tableHeader);
        rpt.Bands[BandKind.Detail].Controls.Add(tableDetail);

    }

    /// <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);
    }

.desgner.cs文件

#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()
    {
        this.Detail = new DevExpress.XtraReports.UI.DetailBand();
        this.TopMargin = new DevExpress.XtraReports.UI.TopMarginBand();
        this.BottomMargin = new DevExpress.XtraReports.UI.BottomMarginBand();
        this.ReportHeader = new DevExpress.XtraReports.UI.ReportHeaderBand();
        this.xrLabel2 = new DevExpress.XtraReports.UI.XRLabel();
        ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
        // 
        // Detail
        // 
        this.Detail.HeightF = 0F;
        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.HeightF = 36.45833F;
        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 = 36.45833F;
        this.BottomMargin.Name = "BottomMargin";
        this.BottomMargin.Padding = new DevExpress.XtraPrinting.PaddingInfo(0, 0, 0, 0, 100F);
        this.BottomMargin.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopLeft;
        // 
        // ReportHeader
        // 
        this.ReportHeader.Controls.AddRange(new DevExpress.XtraReports.UI.XRControl[] {
            this.xrLabel2});
        this.ReportHeader.HeightF = 54.25002F;
        this.ReportHeader.Name = "ReportHeader";
        // 
        // xrLabel2
        // 
        this.xrLabel2.Font = new System.Drawing.Font("宋体", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.xrLabel2.LocationFloat = new DevExpress.Utils.PointFloat(0F, 0F);
        this.xrLabel2.Name = "xrLabel2";
        this.xrLabel2.Padding = new DevExpress.XtraPrinting.PaddingInfo(2, 2, 0, 0, 100F);
        this.xrLabel2.SizeF = new System.Drawing.SizeF(650F, 54.25002F);
        this.xrLabel2.StylePriority.UseFont = false;
        this.xrLabel2.StylePriority.UseTextAlignment = false;
        this.xrLabel2.Text = "标题";
        this.xrLabel2.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
        // 
        // XtraReport3
        // 
        this.Bands.AddRange(new DevExpress.XtraReports.UI.Band[] {
            this.Detail,
            this.TopMargin,
            this.BottomMargin,
            this.ReportHeader});
        this.Margins = new System.Drawing.Printing.Margins(100, 100, 36, 36);
        this.Version = "15.2";
        ((System.ComponentModel.ISupportInitialize)(this)).EndInit();

    } 
    #endregion
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值