Crystal Report In Windows Form Application

Debendra256
Posted by in Windows Forms category on for Beginner level | Points: 250 | Views : 2970 red flag
Rating: 4 out of 5  
 2 vote(s)

Crystal Report is a great Reporting Tool we are using in DotNet to print Reports.Here we will explain how to print a report using Crystal Report in Windows Forms Application.

Introduction

In this article we will explain how to print report using crystal Report in windows application. For using crystal Report in the Windows application please follow the following steps to add it.

Let's start with a windows project named "CanteenManagement" in the solution and  Crystal Report can be added as follows:
Image2-Choose Crystal Reports
Image3:-Choose Standard Report
Image4:-Choose OLEDB(ADO)
Image5:-Choose Microsoft OLEDB Provider for SqlServer.

Image6:-Provide the proper database information.
Image7:-After Successfully connecting with DB add the table where you want to work.
After that if you want to chose column then choose either click finish

Image8:-Now go to the Crystal Report and design as per your Requirenent.

Image9:-Now we have to  show the Report in "Employee Lunch\Dinner Report" so lets go to this menu and add a form there to show report.


Image10:-Add a form as print  as shown below 

Image11:-Add a Crystal Report viewer in the form.

Now here is the code to populate the PRINT page on Clicking "EmployeeLunch\Dinner Booking Entry" menu in MDI parent form.
 private void employeeLunchDinnerReportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Print obj = new Print();
            obj.Show();
        }
Now, when the print page will load the report will generate so just write the following code in crystalReportViewer1_Load event.
 private void crystalReportViewer1_Load(object sender, EventArgs e)
        {
            Print p = new Print();
            Report rpt = new Report();
          
            dt = p.showdata();
            rpt.SetDataSource(dt);
            this.crystalReportViewer1.ReportSource = rpt;
            crystalReportViewer1.Refresh();
        }

Here is showdata method.
 public  DataTable showdata()
        {
            string Query = "select * from BookMeal";

            dt = obj.ShowData(Query);
            if (dt.Rows.Count > 0)
            {
                return dt;
            }
            else
            {
                return dt;

            }

        }

Passing query to one CanteenBAL.cs here is that code.
 public DataTable ShowData(string query)
        {
            da = new SqlDataAdapter(query, CanteenBAL.connection());
            dt = new DataTable();
            da.Fill(dt);
            return dt;
            
        }
Now just save the programme and run the project it will show the report as follow.


Conclusion

In this way we can generate crystal report in windows form.



Page copy protected against web site content infringement by Copyscape

About the Author

Debendra256
Full Name: Debendra Dash
Member Level: Starter
Member Status: Member
Member Since: 10/9/2015 7:13:47 AM
Country: India
Debendra Dash

Nearly 3 years of experiance in various Microsoft Technology.Currently working as a Technical Consultant in R2 international India Pvt Ltd.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)