神刀安全网

Crystal Report In Windows Application Without Database Connection

Crystal Report is a Reporting Tool for creating various reports. It provide a platform to create reports, graphs with user friendly design and also helps to connect with database. This reporting tool have the facility to produce report with connecting database as well as without connecting Database.

Here i will explain how to create a Crystal Report without database connection.

I have the following windows application,

Crystal Report In Windows Application Without Database Connection

In this windows form design when i press the book button after entering data it will directly goes to the DataGrid and will print a good report after " print " button click this is my requirement. So here I will show you the steps to Create this.

Here is the code to bind the data after clicking Book button without database.

Add a dataset and datatable where form Load

public partial class LunchDinnerBookingEntry : Form      {                     static DataSet1 ds = new DataSet1();          DataTable dt = new DataTable();             

Creating a method as Createrow. 

public void createnewrow()         {             if(dt.Rows.Count<=0)             {                    DataColumn dc1 = new DataColumn("PERSONAL NO", typeof(int));                 DataColumn dc2 = new DataColumn("NAME", typeof(string));                 DataColumn dc3 = new DataColumn("DATE", typeof(string));                 DataColumn dc4 = new DataColumn("QUANTITY", typeof(int));                 DataColumn dc5 = new DataColumn("TYPE", typeof(string));                       dt.Columns.Add(dc1);                 dt.Columns.Add(dc2);                 dt.Columns.Add(dc3);                 dt.Columns.Add(dc4);                 dt.Columns.Add(dc5);                          dt.Rows.Add(txt_personalNo.Text, txt_name.Text, txt_date.Text, Convert.ToInt32(txt_quantity.Text), cmb_type.SelectedItem.ToString());                       dataGridView1.DataSource = dt;                 ds.Tables.Add(dt);                }             else             {                    dt.Rows.Add(txt_personalNo.Text, txt_name.Text, txt_date.Text, Convert.ToInt32(txt_quantity.Text), cmb_type.SelectedItem.ToString());                       dataGridView1.DataSource = dt;                                 }          }   

Call the method on Book button click.

private void btn_book_Click(object sender, EventArgs e)         {                          createnewrow();                    }   

Here are the steps to add DataSet and Crystal Report.

STEP 1: Right click on Solution Explorer and select Add New Item and choose a Dataset.

Crystal Report In Windows Application Without Database Connection

After adding dataset it will show the following form.

Crystal Report In Windows Application Without Database Connection

Right click on Dataset Designer and add new Datatable .

Crystal Report In Windows Application Without Database Connection

Add the Columns for the datatable as follows.

Crystal Report In Windows Application Without Database Connection

Now adding dataset is over. Now the second task is to add Crystal Report in the project.Right click on the project, Add New  Item, then Add Crystal Report.

Crystal Report In Windows Application Without Database Connection

Crystal Report type as standard.

Crystal Report In Windows Application Without Database Connection

Select the dataset you created above and its datatable.

Crystal Report In Windows Application Without Database Connection

Now select the datatable to Right side.

Crystal Report In Windows Application Without Database Connection

Click Next and Expand the datatable.

Crystal Report In Windows Application Without Database Connection

STEP 8: Add a new form Print and add a CrystalReporter Viewer in this.

Crystal Report In Windows Application Without Database Connection

When clicking on Print button write the code to show this " Print " form.

private void button1_Click(object sender, EventArgs e)          {              Print obj = new Print();                          obj.Show();          }   

Now go to the Print Form and double click on Crystal Report Viewer and write the following code. 

private void crystalReportViewer1_Load(object sender, EventArgs e)          {              Print p = new Print();                          Token rpt = new Token();              LunchDinnerBookingEntry LBE = new LunchDinnerBookingEntry();              DataSet ds=new DataSet();                 ds = LBE.returndata();              rpt.SetDataSource(ds.Tables["table1"]);              this.crystalReportViewer1.ReportSource = rpt;              crystalReportViewer1.Refresh();          }   

NOTE:

Here the data will come from " LunchDinnerBookingEntry "  to " Print " form so i am creating a method in " LunchDinnerBookingEntry" form which will return me the dataset which is bind to the datagrid. And i have called this from CrystalReportViewer as shown in green color.

Here is the Returndata method.

public DataSet returndata()           {               return ds;           }   

As the dataset is declared globally so will take all the data we have entered. Now design the report header and footer.

Now run the application.

Crystal Report In Windows Application Without Database Connection

Now click the print button to print the report.

Crystal Report In Windows Application Without Database Connection

I have designed the report with header and footer of the report and it will print the report as shown above.

Read more articles on Crystal Reports :

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Crystal Report In Windows Application Without Database Connection

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
分享按钮