Monday, March 1, 2010

Report Writing in Windows Forms

Microsoft Report is based on report definition which is an XML file that gives us the data of a report. It may have different extensions. For eg. We can create a client-side report definition language (*.rdlc) file using Visual Studio. Reports also include tables, aggregated and multidimensional data, charts etc as per users requirements.

Let us see a example of creating a report. In this we will have a table in our report which will shoe us details of students like name,roll no etc.. Lets start is step by step..

Step 1:
We want to build a windows application so we will choose Windows Form Application. You can select any language you want according to your choice. May be it is VB or C#. Give it a name and move ahead by clicking ok. You can check it in screenshot given below. I have named it as SampleSQLReport.



Step 2:
After creating Windows Form Application we will get a form which will be completely blank. Now we will ask user to enter his name who wants to generate a report. For this we will need a label on which we will ask user to enter name, then a textbox in which user will enter their name, and last a button on clicking of which will generate a report. We can also increase form size by going into its properties and changing size of it. All these things are shown in screenshot below.



Step 3:
Now as we want to generate report we need to include a Microsoft ReportViewer. We can also use Crystal ReportViewer but here we will use Microsoft ReportViewer. We can get this from our toolbox which is on left side. In that we will get it from Reporting. So you can include Microsoft ReportViewer from there by dragging and dropping or by double clicking on it. You can see a report just below label, textbox etc. We can see all these things in screenshot below.



Step 4:
After this we have to add a file named .rdlc creating report. For adding this we need to add new item  Report. Name it. Here I have given its name as rptSample.rdlc. After adding it we get this in window as shown below.



Step 5:
Next thing we will do is that the textbox which we had asked on main application we will display that name on report too.. For that we need to put textbox on report too which will display name. For displaying name on this from main page textbox we need to add report parameters. We can do this by going into Report tab in above navigation bar. Report  Report Parameters. After you select this you will get a screen which is shown in below screenshot. In similar way you have to move ahead.



In above screenshot you can see we have added parameters. I have given its name as paraName. Similarly you can add your parameters.

In below screenshot we can see how to add parameters to textboxes so that its value gets displayed in textbox.



Step 6:
As we see above we have to select (Category) Parameters  (Item) All  (Parameter) parameter name which you have declared before in your code. Then after clicking ok you will get below screen.




Till what we have done, its code is given in below screenshot.



Till what we have done lets run it. So that we will get to know whether wat we did atleast till that point how its working. So after running you get this.



Step 7:
Now lets move ahead with database part as we want to display Student database in a tabular format in our report. So for that will add annother class in code which will include Student table format. We can see the code in below screenshot.



In above code DataTable Student is created with rows like rollno,name,div,marks and those columns are added in columns.

Step 8:
Now we have created DataTable but we need to add DataSource also to this. For that we have an option on left hand named data source. When you click on that we get an option Add New Data Source. After clicking on that we get screen as shown below and in that we have to choose object.





After selecting object click Next. After that you will get screen as shown below.



Step 9:
In aboce screenshot you can see options in which student option is included. It’s the same table name which we have created in our code. Select that and click Next. On left side you will see table and its columns which you had added in code. Now as we want to display this in tabular format we will dragand drop Table from toolbox on right side and we will get a table in our report page. Now we can see rows and columns in that table. We will select each column from left side and drag and drop it on particular column in the table. For eg. We will drag and drop rollno column and drag and drop it on 1st column of the table. Similarly we will do of remaining rows and this way we have added columns to our table. If you want to add more columns to the table you can right click on it and you will get option Insert column to right/left. So according to your needs you can add more columns to your table. Final output of this is shown in below screenshot.





Step 10:
Now we have to add some data in Student table. We will have to add this in code itself. For that we have to write code written in below screenshot marked in red.



Final Output:
After adding this data our work is finished. Now we will run our project and we will get output as shown in screenshot below.



So in this way you can create your reports.

No comments:

Post a Comment