Tìm kiếm Blog này

Tạo crystal report trong C# .Net



Giới thiệu
Hiện tại có rất ít bài tiếng việt hướng dẫn về phần này.
" creating Crystal Reports using C#.NET" .
Tạo một Crystal Reports trong C#.Net với CSDL access
SQL sercer làm tương tự.
Tạo dataset
Đầu tiên bạn tạo 1 dự án
Sau đó
“Add New Item” trong “Project” menu, bạn add a Dataset
Giống hình bên dưới.
Trích:
String connString = @"Provider=Microsoft.Jet.OLEDB.4.0;_
Data Source=..\\..\\myDB.mdb;User ID=Admin;Password=";

OleDbConnection conn = new OleDbConnection(connString);
conn.Open();
string query = "SELECT studentID, firstName, lastName, birthDate, _
address, contactNo FROM studentInfo";
OleDbDataAdapter oleDA = new OleDbDataAdapter(query,conn);
Tạo một báo cáo dùng Crystal Reports
Sau khi tao xong dataset bạn add a Crystal Report vào project bằng cách “Add New Item” trong “Project” menu. Sau đó chọn tên xuất hiện rồi nhấn nút ok.
Màn hình sẽ hiển thị như sau bạn chọn mặt định rồi ấn OK
Sau đó chọn table rùi insert table
phần này có thể khác so với VB2005 nhưng cũng tương tự thôi!
sau đó chọn các fields hiển thị trong report --> nhần nút add
Xong xui bạn nhấn nút next-->cuối cùng chọn style kiểu bạn muốn report hiển thị
Kế tiếp bạn sẽ cho report hiển thị trên form
Bạn tao một form và add crystalReportViewer
viết code trong thuộc tính page load
Trích:
// code to get data from the DB
DBConnection DBConn = new DBConnection();
OleDbDataAdapter myDataAdapter = DBConn.getDataFromDB();

// use the created Dataset to fill it with data got
// from the DB by using the DataAdapter
DataSet dataReport = new DataSet();
myDataAdapter.Fill(dataReport,"myPersonalInfoTable ");
// create a new report from the created CrystalReport
myDataReport myDataReport = new myDataReport();

// set the data source of the report
myDataReport.SetDataSource(dataReport);

// set the report source of the created “crystalReportViewer”
// component to the created report
crystalReportViewer1.ReportSource = myDataReport;
bạn có thể thay đổi dường dẫn mặt định bằng
Trích:
crystalReportViewer1.ReportSource = @“..\Reports\salesReport.rpt”;
Link file demo: