devExpress打印预览

devExpress的打印预览方法分两种方式:web方式和winform方式。

一、web方式

    1、添加web窗体,名为preview.aspx.cs,拖入dev的ReportToolbar控件,名为ReportTool1和ReportViewer控件,名为ReportViewer1。设置ReportTool1的ReportViewerID的属性值为ReportViewer1。

    2、preview.aspx.cs的后台代码

1
2
3
4
5
6
7
8
9
protected void Page_Load( object sender, EventArgs e)
   {
       if (Page.Request[ "id" ] != null )                                  //id为传值
       {
           int id = int .Parse(Page.Request[ "id" ].ToString());
           XtraReport2 report = new XtraReport2(id);
           ReportViewer1.Report = report;
           report.DataSource = bll_purchaseOrder.GetList( " id=" + id);
       }
 
 

    3、添加devExpress的Report Class,名为XtraReport2.cs。在报表模版中拖入各个控件。

    4、XtraReport2.cs的后台代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public XtraReport2( int id)
{
     InitializeComponent();
     dataBind(id);
}
private void dataBind( int id)
{
     lblCode.DataBindings.Add( "Text" ,DataSource, "code" );
     lblSupplier.DataBindings.Add( "Text" ,DataSource, "suppliersName" );
     lblDate.DataBindings.Add( "Text" ,DataSource, "purchaseDate" , "{0:yyyy-MM-dd}" );
     lblHandle.DataBindings.Add( "Text" ,DataSource, "handlePerson" );
     lblRemark.DataBindings.Add( "Text" ,DataSource, "remark" );
     lbCreatePerson.DataBindings.Add( "Text" ,DataSource, "createName" );
     lbCreateDate.DataBindings.Add( "Text" ,DataSource, "createDate" , "{0}" );
     lbAuditPerson.DataBindings.Add( "Text" ,DataSource, "auditName" );
     lbAuditDate.DataBindings.Add( "Text" ,DataSource, "auditDate" );
     pgOrderDetail.DataSource = bll_purchaseOrderDetail.GetListByOrderId(id);   //pgOrderDetail为Dev的XtraPivotGrid控件
}
  
//事件
private void pgOrderDetail_FieldValueDisplayText( object sender, DevExpress.XtraReports.UI.PivotGrid.PivotFieldDisplayTextEventArgs e)
{
     //将控件的“Grand Total”汉化成"合计"
     if (e.ValueType == DevExpress.XtraPivotGrid.PivotGridValueType.GrandTotal)
         if (e.DisplayText == "Grand Total" )
             e.DisplayText = "合计:" ;
}
   


二、winform方式

    winform方式有两种,一种是直接弹出dev的预览模版,另一种是弹出自定义的预览窗口。

    1、直接弹出dev的预览模版的方式

    (1)添加Dev的Report Class,名为XtraReport1.cs,拖入各个控件。

    (2)在有打印预览按钮的窗口,打印预览按钮的点击事件代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private void barLargeButtonItem1_ItemClick( object sender, DevExpress.XtraBars.ItemClickEventArgs e)
         {
             DataTable dt = new DataTable();
             DataColumn dcTheme = new DataColumn( "theme" , typeof ( string ));
  
             dt.Columns.Add(dcTheme);
             dt.Columns.Add(dcMain);
  
             DataRow dr = dt.NewRow();
             dr[ "theme" ] = lib_company.getCompanyByID(frmMain.GUID, int .Parse(co_title.EditValue.ToString())).Company;
             dr[ "main" ] = rec_Main.HtmlText;
             dt.Rows.Add(dr);
  
             XtraReport1 report = new XtraReport1();
             report.DataSource = dt;
             report.dataBind();
             ReportPrintTool printTool = new ReportPrintTool(report);
             printTool.ShowPreviewDialog();
         }
   

    (3)XtraReport1的后台代码

1
2
3
4
5
6
7
8
9
public XtraReport1()
   {
       InitializeComponent();
   }
public void dataBind( string company)
   {
       xlblTheme.DataBindings.Add( "Text" ,DataSource, "theme" );
       xrtMain.DataBindings.Add( "Html" ,DataSource, "main" );
   }
 


    2、弹出自定义的预览窗口

    (1)添加dev的Report Class,名为XtraReport1.cs。拖入各个控件。

    (2)XtraReport1.cs的后台代码

1
2
3
4
5
6
7
8
9
public XtraReport1()
  {
      InitializeComponent();
  }
  public void dataBind( string company)
  {
      xlblTheme.DataBindings.Add( "Text" ,DataSource, "theme" );
      xrtMain.DataBindings.Add( "Html" ,DataSource, "main" );
  }

     (3)添加Windows窗体,名为preview.cs,拖入dev的Reporting部分的documentViewer控件,在该控件右上方的三角菜单中选择“create standard toolbar”。

     (4)preview.cs的后台代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public partial class preview : Form
  {
      DataTable dt;
      public preview(DataTable dt1)
      {
          InitializeComponent();
          dt = dt1;
      }
 
      private void view_Load( object sender, EventArgs e)
      {
          XtraReport1 report = new XtraReport1();
          report.DataSource = dt;
          report.dataBind();
          this .documentViewer1.PrintingSystem = report.PrintingSystem;
          report.CreateDocument();
      }
  }

    (5)在有打印预览按钮的窗口,打印预览按钮的点击事件代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private void barLargeButtonItem1_ItemClick( object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
     DataTable dt = new DataTable();
     DataColumn dcTheme = new DataColumn( "theme" , typeof ( string ));
     DataColumn dcMain = new DataColumn( "main" , typeof ( string ));
  
     dt.Columns.Add(dcTheme);
     dt.Columns.Add(dcMain);
  
     DataRow dr = dt.NewRow();
     dr[ "theme" ] = lib_company.getCompanyByID(frmMain.GUID, int .Parse(co_title.EditValue.ToString())).Company;       
     dr[ "main" ] = rec_Main.HtmlText;
     dt.Rows.Add(dr);
  
     preview preview = new preview(dt);
     preview.ShowDialog();
}

你可能感兴趣的:(DevExpress)