Dynamically Creating Crystal Reports
Số trang: 3
Loại file: pdf
Dung lượng: 14.85 KB
Lượt xem: 10
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
[ Team LiB ] Recipe 7.16 Dynamically Creating Crystal Reports Problem You need to define a DataTable at runtime and bind it to a Crystal Report. Solution Create a DataAdapter and use it to fill a DataTable with a subset of records
Nội dung trích xuất từ tài liệu:
Dynamically Creating Crystal Reports [ Team LiB ]Recipe 7.16 Dynamically Creating Crystal ReportsProblemYou need to define a DataTable at runtime and bind it to a Crystal Report.SolutionCreate a DataAdapter and use it to fill a DataTable with a subset of records (specified bya range of OrderID values, from the Orders table joined to Order Details records from theNorthwinds sample database demonstrated in the following example). Create a newreport document and set its data source to the DataTable. To display the report, set thesource of the report view to the report document.The C# code is shown in Example 7-32.Example 7-32. File: CrystalReportsForm.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Windows.Forms;using CrystalDecisions.CrystalReports.Engine;using CrystalDecisions.Shared;using System.Data;using System.Data.SqlClient;private CrystalDecisions.Windows.Forms.CrystalReportViewer crv;// . . .// Get the user entered OrderID range.int orderIdFrom, orderIdTo;try{ orderIdFrom = Convert.ToInt32(orderIdFromTextBox.Text); orderIdTo = Convert.ToInt32(orderIdToTextBox.Text);}catch (Exception ex){ MessageBox.Show(ex.Message, Dynamic Crystal Reports, MessageBoxButtons.OK, MessageBoxIcon.Error); return;}Cursor.Current = Cursors.WaitCursor;// Create a DataAdapter and fill the table.String sqlText = SELECT * FROM Orders + JOIN [Order Details] Order_Details ON Orders.OrderID = + Order_Details.OrderID + WHERE Orders.OrderID BETWEEN + orderIdFrom + AND + orderIdTo;SqlDataAdapter da = new SqlDataAdapter(sqlText, ConfigurationSettings.AppSettings[Sql_ConnectString]);DataTable dt = new DataTable( );da.Fill(dt);// Create a new ReportDocument.ReportDocument cr = new ReportDocument( );// Load the report.cr.Load(ConfigurationSettings.AppSettings[Project_Directory] + @Chapter 07\OrderWithDetailsCrystalReport.rpt);// Set the data source for the report.cr.SetDataSource(dt);// Set the report document for the report view.crv.ReportSource = cr;Cursor.Current = Cursors.Default;DiscussionFollow these steps to use a DataTable created at runtime as the data source for a CrystalReport: 1. Using the Crystal Report Designer in Visual Studio.NET, design and create the Crystal Report .RPT file. Link the report to the database in the designer to get the fields for the report as would normally be done. For more information about using the Crystal Report Designer, see the MSDN Library (you might have to change the filter in MSDN to (no filter) from .NET Framework). 2. In the application, use a DataAdapter to fill a DataTable with the data required by the report. 3. Create a new ReportDocument object: ReportDocument cr = new ReportDocument( ); The ReportDocument class represents a report and contains methods and properties including those used define, format, and load the report. 4. Use the Load( ) method of the ReportDocument to load the report defined in step 1: 5. cr.Load(ConfigurationSettings.AppSettings[Project_Directory] + @Chapter 07\OrderWithDetailsCrystalReport.rpt); 6. Use the SetDataSource( ) method of the ReportDocument to pass the data source to the report engine: cr.SetDataSource(dt); 7. Set the ReportSource property of the CrystalReportViewer to the ReportDocument to display the report in the viewer: crv.ReportSource = cr;The CrystalReportViewer class provides methods, properties, and events that allowcontrol of viewer appearance and functionality.[ Team LiB ]
Nội dung trích xuất từ tài liệu:
Dynamically Creating Crystal Reports [ Team LiB ]Recipe 7.16 Dynamically Creating Crystal ReportsProblemYou need to define a DataTable at runtime and bind it to a Crystal Report.SolutionCreate a DataAdapter and use it to fill a DataTable with a subset of records (specified bya range of OrderID values, from the Orders table joined to Order Details records from theNorthwinds sample database demonstrated in the following example). Create a newreport document and set its data source to the DataTable. To display the report, set thesource of the report view to the report document.The C# code is shown in Example 7-32.Example 7-32. File: CrystalReportsForm.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Windows.Forms;using CrystalDecisions.CrystalReports.Engine;using CrystalDecisions.Shared;using System.Data;using System.Data.SqlClient;private CrystalDecisions.Windows.Forms.CrystalReportViewer crv;// . . .// Get the user entered OrderID range.int orderIdFrom, orderIdTo;try{ orderIdFrom = Convert.ToInt32(orderIdFromTextBox.Text); orderIdTo = Convert.ToInt32(orderIdToTextBox.Text);}catch (Exception ex){ MessageBox.Show(ex.Message, Dynamic Crystal Reports, MessageBoxButtons.OK, MessageBoxIcon.Error); return;}Cursor.Current = Cursors.WaitCursor;// Create a DataAdapter and fill the table.String sqlText = SELECT * FROM Orders + JOIN [Order Details] Order_Details ON Orders.OrderID = + Order_Details.OrderID + WHERE Orders.OrderID BETWEEN + orderIdFrom + AND + orderIdTo;SqlDataAdapter da = new SqlDataAdapter(sqlText, ConfigurationSettings.AppSettings[Sql_ConnectString]);DataTable dt = new DataTable( );da.Fill(dt);// Create a new ReportDocument.ReportDocument cr = new ReportDocument( );// Load the report.cr.Load(ConfigurationSettings.AppSettings[Project_Directory] + @Chapter 07\OrderWithDetailsCrystalReport.rpt);// Set the data source for the report.cr.SetDataSource(dt);// Set the report document for the report view.crv.ReportSource = cr;Cursor.Current = Cursors.Default;DiscussionFollow these steps to use a DataTable created at runtime as the data source for a CrystalReport: 1. Using the Crystal Report Designer in Visual Studio.NET, design and create the Crystal Report .RPT file. Link the report to the database in the designer to get the fields for the report as would normally be done. For more information about using the Crystal Report Designer, see the MSDN Library (you might have to change the filter in MSDN to (no filter) from .NET Framework). 2. In the application, use a DataAdapter to fill a DataTable with the data required by the report. 3. Create a new ReportDocument object: ReportDocument cr = new ReportDocument( ); The ReportDocument class represents a report and contains methods and properties including those used define, format, and load the report. 4. Use the Load( ) method of the ReportDocument to load the report defined in step 1: 5. cr.Load(ConfigurationSettings.AppSettings[Project_Directory] + @Chapter 07\OrderWithDetailsCrystalReport.rpt); 6. Use the SetDataSource( ) method of the ReportDocument to pass the data source to the report engine: cr.SetDataSource(dt); 7. Set the ReportSource property of the CrystalReportViewer to the ReportDocument to display the report in the viewer: crv.ReportSource = cr;The CrystalReportViewer class provides methods, properties, and events that allowcontrol of viewer appearance and functionality.[ Team LiB ]
Tìm kiếm theo từ khóa liên quan:
công nghệ thông tin kỹ thuật lập trình Oreilly Ado Dot Net Cookbook Ebook-Lib Displaying an Image from a Database in a Web Forms ControlGợi ý tài liệu liên quan:
-
52 trang 417 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 301 0 0 -
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 287 0 0 -
74 trang 283 0 0
-
96 trang 283 0 0
-
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 270 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 267 1 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 260 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 252 0 0 -
Tài liệu hướng dẫn sử dụng thư điện tử tài nguyên và môi trường
72 trang 251 0 0