Thứ Sáu, 15 tháng 3, 2013

C# Crystal Reports without database

http://csharp.net-informations.com/crystal-reports/csharp-crystal-reports-without-database.htm

BCT: đã thực hiện theo cách này và đáp ứng tốt nhu cầu tạo báo cáo bằng Crystal Report
- Đã thử 3 cách để truyền dữ liệu vào trong báo cáo lúc runtime
    + Truyền thẳng vào DataSourceConnections của báo cáo, tạo query hoặc command trực tiếp trên báo cáo
    + Tạo một nguồn dữ liệu trả về IDataReader và dịch ra DLL, sau đó gán vào làm nguồn dữ liệu của báo cáo
    + Tạo một dataset để gán làm nguồn dữ liệu lúc thiết kế báo cáo, sau đó gán một dataset lúc runtime
- Cách tạo Dataset và gán làm nguồn dữ liệu để thiết kế báo cáo là cách dễ kiểm soát nhất
- Nếu tạo một DLL để làm nguồn dữ liệu thì khi dịch DLL này nhiều khi bị khó khăn vì không thể ghi đè lên tệp DLL có sẵn (đang được sử dụng bởi báo cáo lúc thiết kế). Ngoài ra cũng chưa thử thành công việc thay đổi nguồn dữ liệu (mặc dù debug vẫn thấy dữ liệu thay đổi, nhưng khi hiển thị thì báo cáo vẫn lấy dữ liệu hệt như lúc thiết kế - nghĩa là nguồn dữ liệu gán lúc runtime không có ý nghĩa gì)
- Cách tạo tham số cho command và gán kết nối vào báo cáo thì không tận dụng được các query và function trong cơ sở dữ liệu
- Các bước thực hiện:
    + Tạo nguồn dữ liệu Dataset cho báo cáo
    + Gán nguồn dữ liệu và thiết kế báo cáo trên Visual Studio như bình thường
    + Trong mã nguồn khi gọi báo cáo, tạo Dataset chứa dữ liệu và gán vào thuộc tính DataSource của báo cáo.

C# Crystal Reports without database

SiteMap
CSharp.Net-Informations.com

Usually we are using Crystal Reports to fetch data from databases and show it as a report. Here we are generating a Crystal Reports from C# without using a database . For generating a Crystal Reports without database , here we are using a Strongly Typed Dataset and a Data Table of C#.

If you are new to Crystal Reports and do not know how to create Crystal Reports from C# , please take a look at the section step by step tutorial for creating a Crystal Reports from C#.

Generating a Strongly Typed DataSet

Create a new C# Project and create a Dataset from Project - Add New Item Dialogue Box.

Select Dataset from list

Accept the default name DataSet1.xsd .

Create a data table for DataSet1.xsd in C#.

Select DataSet1.xsd from Solution Explorer and right click . Select datatable from the menu. Then you will get a datatable in the Datast . Right click the datatable and select Add-Column .

Here we are making a two column Crystal Reports , so we need two column in the data table . Add and ID column and Item column in the Data Table.

Now the dataset part is over . Next step is to create a Crystal Reports from the Dataset we created. Select a new Crystal Reports from Add New Item menu and accept the default settings. The next screen is to select appropriate data source . There you can find the Datatable1 from Project data - ADO.NET Datasets , and select Datatable1 to the right side.

Click Next button and select ID and Item from the datatable1 to right side and click finish.

Now the C# Crystal Reports designer part is over . Next part is to create data for the Crystal Reports . For that we have to create a Data Table through programmatically and add data to dataset1.

Select the default form (Form1.cs) you created in C# and drag a button and a CrystalReportViewer control to your form .

Copy and paste the following source code and run your C# project


using System;  using System.Windows.Forms;  using CrystalDecisions.CrystalReports.Engine;  using CrystalDecisions.Shared;  using System.Data;    namespace WindowsApplication1  {      public partial class Form1 : Form      {            public Form1()          {              InitializeComponent();          }            private void button1_Click(object sender, EventArgs e)          {              DataSet1 ds = new DataSet1();              DataTable t = ds.Tables.Add("Items");              t.Columns.Add("id", Type.GetType("System.Int32"));              t.Columns.Add("Item", Type.GetType("System.String"));                DataRow r ;              int i = 0;              for (i = 0; i <= 9; i++)              {                  r = t.NewRow();                  r["id"] = i;                  r["Item"] = "Item" + i;                  t.Rows.Add(r);              }                CrystalReport1 objRpt = new CrystalReport1();              objRpt.SetDataSource(ds.Tables[1]);              crystalReportViewer1.ReportSource = objRpt;              crystalReportViewer1.Refresh();           }      }  }

Không có nhận xét nào:

Đăng nhận xét

(Chơi cho vui) AIRDROP CHAINGE FINANCE - dự án xây dựng ứng dụng ngân hàng số cho mọi người

 Không hiểu lắm về cái này, tuy nhiên thấy quảng cáo khá nhiều, lại chỉ cung cấp vài thông tin cá nhân (mà mấy ông lớn như facebook với goog...