Introduction
The reason I’m making this how-to is, that I had a project
which involved making a report (for the first time), and it had to be made using
several tables from a database. I searched the web for answers but I didn’t
find any useful or easy solution. I found answers where it was almost
impossible to use more than one table in making reports. I’m sure that there
are some solutions out there, but I didn’t find them. I was almost ready to give
up after all these “good solutions” I found on the internet.
It took me some time to figure out how to make a report
using several tables.
For this little project, you’ll need:
- A database with several tables
- Visual Studio 2010 (or 2008)
Let’s start the project:
- Open VS and choose New Project…:
- Choose Windows Forms Application and give it a name, in my case, it is “Tables2Report”…
- When Form1 appears, we will choose Add New Data Source:
- Choose “Database”, ”Next” -> “Dataset”, “Next” -> “New
Connection…” and here choose your data source, server name, database name, and choose “Test Connection” to make sure that your choice is the right one.
Click on OK. Now you have the connection to the database. Click on Next and give a name to the Connection String and click Next.
- Now choose your “Tables” and write a name for your DataSet and click Finish:
- Now you should have the Dataset with the tables in the Data Sources tab:
- Right click on the empty place in the Data Sources tab and choose Edit DataSet with Designer:
- Now you have the tables you chose.
- Right click on the empty place in the window and choose Add -> Table Adapter…:
- Choose your data connection (it will be the connection you saved earlier, and it has to be with the (Settings) at
the end of the name. Click on Next two times and click on Query Builder…
- Choose your tables. Ctrl + Mouse left click on tables. Click on Add and Close.
- Now choose the columns you want to be on the report and press OK:
- Now show a window with the SQL statement. Press Finish to close the window and a new table is shown with the name Data Table1 with the columns you chose earlier.
- Click on Form1.cs [Design] tab to show the Form.
- From Toolbox, drag the ReportViewer icon to
Form1
.
- Arrange the size of the form and Report Viewer as you wish.
- On the upper right corner of Report Viewer, you will find a little white button with a triangle on it. Click on it with the mouse and you will get a little menu, there you will
find Design a new report, choose it:
- Now you have the report wizard in front of you. Choose the Data Source you saved earlier,
and from the Available datasets choose Data Table1 and press Next.
- Now you can arrange rows and values. It is easier to move everything to the Values window and press
Next twice and Finish at last.
- You have your report with the new table.
- There is one thing to make sure before you’re ready to run your report software. Go back to
Form1 [Design] and on the upper right corner of Report Viewer, push the little
mark and press Choose Report’s triangle and choose the ……..rdlc.
- You’re ready to run the program.