首先新建一个XtraReport类。根据需要设计报表页面布局;

布局设计完毕后,写代码绑定数据;
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using DevExpress.XtraReports.UI;
- using System.Data;
- using Zeda.AssistantClass;
-
- namespace LYWJMIS
- {
- public partial class MyReport2 : DevExpress.XtraReports.UI.XtraReport
- {
- private DataRow drPur;
-
- public MyReport2()
- {
- InitializeComponent();
- }
-
-
-
-
- public MyReport2(DataRow drPur)
- : this()
- {
- this.drPur = drPur;
- string sheetID = string.Empty;
- if (drPur == null) return;
-
- BindFormData(drPur);
-
- sheetID = drPur["ID"].ToString();
-
- DataSet dsDetail = DataService.Instance.GetPurchaseSheetDetailInfoBySheetID(sheetID);
-
- BindTableData(dsDetail);
- }
-
-
-
- private void BindTableData(DataSet ds)
- {
-
- this.xrTableCell1.DataBindings.Add("Text", ds, "DB0137A");
- this.xrTableCell2.DataBindings.Add("Text", ds, "DB0152A");
- this.xrTableCell3.DataBindings.Add("Text", ds, "DB0150A");
- this.xrTableCell7.DataBindings.Add("Text", ds, "DB0151A");
- this.xrTableCell8.DataBindings.Add("Text", ds, "DB0168A");
- this.xrTableCell9.DataBindings.Add("Text", ds, "DB0183A");
- this.xrTableCell10.DataBindings.Add("Text", ds, "DB0188A", "{0:n2}");
- this.xrTableCell11.DataBindings.Add("Text", ds, "DB0354A", "{0:f0}");
-
- this.xrTableCell23.DataBindings.Add("Text", ds, "DB0354A", "{0:f0}");
- this.xrTableCell23.Summary = new XRSummary(SummaryRunning.Page, SummaryFunc.Sum, string.Empty);
-
- this.xrTableCell18.DataBindings.Add("Text", ds, "DB0354A", "{0:f0}");
- this.xrTableCell18.Summary = new XRSummary(SummaryRunning.Group, SummaryFunc.Sum, string.Empty);
- }
-
-
-
- private void BindFormData(DataRow dr)
- {
- DataSet ds = DataSetOperator.DataRowToDataSet(dr);
-
- this.txtDB0336A.Text = dr["DB0336A"].ToString();
-
- this.txtDB0337A.DataBindings.Add(new XRBinding("Text", ds, "DB0337A", "{0:yyyy-MM-dd}"));
- this.txtDB0005A.Text = dr["DB0005A"].ToString();
- this.txtDB0339A.Text = dr["DB0339A"].ToString();
- this.txtDB0345A.DataBindings.Add(new XRBinding("Text", ds, "DB0345A", "{0:n2}"));
- this.labPrintTime.Text = DateTime.Now.Date.ToString();
-
- }
- }
- }
调用MyReport2报表类打印预览
- private void btnPrintReport_Click(object sender, EventArgs e)
- {
- DataRow dr = this.wgcPur.GridView1.GetFocusedDataRow();
- if (dr == null) return;
- MyReport2 rep = new MyReport2(dr);
-
- rep.PaperKind = System.Drawing.Printing.PaperKind.Custom;
-
- double width = 24.1 * 0.3937008 * Dpi.GetDeviceCapsX();
- double height = 9.3 * 0.3937008 * Dpi.GetDeviceCapsY();
- rep.PageSize = new System.Drawing.Size((int)width, (int)height);
-
- rep.ShowPreview();
- }

xrTableCell属性:
- TextAlignment:设置单元格显示内容的对齐方式;
- Text:设置单元格显示的字符;
- Styles-Style:设置单元的样式;
- CanGrow:设置单元中的内容是否能够换行。如果设置为true,则内容超出单元格长度时,会自动换行,同时,单元格高度自动增加;
- CanShrik:设置单元格的高度是否会随内容伸缩;
- Borders:设置单元格显示上、下、左、右的边框。
XtraReport属性:
- PaperKind:设置打印报表纸张的类型;
- PageHeight:设置报表的纸张的高度(单位:像素)。注意:只有当PaperKind=Custom时,才能设置此属性,否则无效;
- PageWidth:设置纸张的宽度(单位:像素)。注意:只有当PaperKind=Custom时,才能设置此属性,否则无效;
- PageColor:设置报表的背景颜色;
- Margins:设置纸张的页边距;
- DefaultPrinterSettingsUsing:打印报表是否应用默认打印机的默认设置(纸张类型、页边距等);
- UseMargins:如果值为true,则属性Margins将失去作用;
- UsePaperKind:如果值为true,则属性PerperKind,PageHeight,PageWidth将失去作用;
- GroupFooter-RepeatEveryPage:如果值为true,则页脚将在每一页显示;如果为false,则页脚只在第一页显示
转载地址:http://blog.csdn.net/ljunqiang/article/details/39498171#comments