Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.18 MB, 71 trang )
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
using
using
using
using
System;
System.Windows.Forms;
CrystalDecisions.CrystalReports.Engine;
CrystalDecisions.Shared;
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();
}
}
}
14. C# Crystal Reports from SQL Query
The following section describes how to create a Crystal Reports in C# using
SQL Query String .
All C# Crystal Reports Tutorial in this website is based on the following
database - crystaldb. So before you begin this section , please take a look at
the database structure of crystaldb - Click Here C# crystaldb
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
Here we are using a Strongly Typed Dataset for generating a Crystal Reports
from SQL Query string . Before start this section , you can take look at the
previous section of how to create a C# Crystal Report from Strongly Typed
Dataset. Because here we are using Strongly Typed Dataset for generating
Crystal Report from SQL Query string.
Here we are generating a report against the Product table . So we are passing
the following sql and generating the report
sql = "SELECT Product_id,Product_name,Product_price FROM Product";
Create a new C# project and create a new Strongly Typed Dataset from
Project - Add New Item Dialogue Box.
Add three column in the Strongly Typed Dataset .
Product_id
Product_name
Product_price
Create a new Crystal Report and select DataTable as Data Source . You can
select DataTable from the wizard , Project Data - ADO.NET Dataset Crystal report Dataset1 - dataset1. Click the Next Button.
Select fields ( Product_id , Product_name , Product_price ) from the next