Reporting with Windows Forms

72 388 0
Reporting with Windows Forms

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Reporting with Windows Forms S o far, this book has covered the theory of client-side reporting services and various report- ing patterns used in real-world situations. Since I feel you’re eager to start with the practical part of this book, the rest of the book is hands-on in nature. There are many practical projects coming your way to help you master the craft of client-side reporting with Visual Studio. Going through the chapters that discuss theory and provide a general introduction is different from going through the chapters with practical content. Before you start reading this chapter, I’d suggest that you get in front of your computer. Why? Because what I’m going to discuss here is not just narrative; it’s a step-by-step walk-through designed to make you a report-developing champion. I’m going to keep this hands-on approach as simple as possible. We’ll start with the basic knowledge needed to build the host client, that is, a Windows Forms client. After that, we’ll work on several real-world practical reporting projects. Each project targets a business case and is based on a reporting pattern, which we discussed in previous chapters. This chapter will cover • “Windows Forms 101,” a step-by-step tutorial for using Windows Forms • A variety of reporting projects • Troubleshooting tips • Exercises for you to practice Windows Forms 101 Let’s start the journey with a quick tutorial on Windows Forms. I know you might be thinking, “ This book is about client-side repor ting; why do I need to know Windows Forms?” Well, since we’re going to host our reports with Windows Forms, it is important for you to know the mini- mum requirements to host your reports and present them to the user. All r ight folks, the moment has arr ived; let’s roll up our sleeves to do some serious key- boarding and dragging and dropping. If you’re comfortable with creating a Windows Forms project and know how to add the ReportViewer to a project, you may jump directly to the first pr oject. P lease make sur e y ou ’ v e pr operly installed Microsoft Visual Studio 2005 on your com- puter before you continue to the next section. If you are new to the Visual Studio IDE, I have cr eated a br ief intr oduction for you in Appendix A; please read it before moving forward. 75 CHAPTER 4 8547ch04final.qxd 8/30/07 4:08 PM Page 75 Creating a Windows Forms Project Please open Visual Studio, and use the following steps, illustrated in Figure 4-1, to create a Windows application project: 1. Click File ➤ New ➤ Project, or press the hot keys Ctrl+Shift+N. 2. In the “Project types” pane of the New Project dialog box, select Visual C# ➤ Windows. 3. In the Templates pane, select Windows Application. 4. Please give the application a name; I’ve called the project RSWin101. You may choose a different location for storing the application files according to your preference. 5. Click the OK button to finish the process. After you click OK, Visual Studio will create a new Windows application project. You’ll also notice that a new form with the name Form1 is part of the project (see Figure 4-2). Figure 4-1. Create a new Windows Forms application After you finish creating the project, you should see something similar to Figure 4-2. However, depending on your current IDE settings, you might see some toolboxes hidden or floating (please see the Visual Studio help files to learn how to customize the look and feel of the IDE). Anyway, you should see the blank Form1 created for you with the new project. CHAPTER 4 ■ REPORTING WITH WINDOWS FORMS76 8547ch04final.qxd 8/30/07 4:08 PM Page 76 Figure 4-2. The Visual Studio 2005 IDE with the RSWin101 project loaded ■ Tip You can always make the Toolbox window visible if you don’t see it in the IDE by clicking View ➤ Toolbox or pressing Ctrl+Alt+X. To get the maximum amount of space on the design surface, you may want to use the auto-hide feature of toolboxes (see Appendix A to learn how to access the auto-hide feature). Let’s set the properties of Form1 according to the values in Table 4-1. We need to set the size property carefully to make sure we have enough space in Form1 for a complete view of the report; specifically, we need to make sure it’s wide enough. While applying the property settings, if the property window is not visible in the IDE, you may press the F4 key to make it visible. Pease make sure to select Form1 before applying changes to properties using the prop- erty window. Table 4-1. Properties of Form1 Property Value Text Windows Forms Host Client S iz e 750, 500 I’m assuming that you have at least 800 ✕ 600 resolution set up on your computer. There- for e , I’m trying to get the maximum width of the form (750 pixels) available for viewing during r un time . P lease feel fr ee to adjust or apply any other pr operties according to your needs. CHAPTER 4 ■ REPORTING WITH WINDOWS FORMS 77 8547ch04final.qxd 8/30/07 4:08 PM Page 77 Adding the ReportViewer to the Form Why do we need a ReportViewer? Just as we need a DVD player to play a DVD, we need the viewer to preview the report. ReportViewer gives life to your reports. R eportViewer not only allows you to preview the output but also helps you to produce the information in the various popular formats (e.g., PDF file and Excel spreadsheet). You can also print a hard copy of the report while you’re viewing the output. Let’s start by dragging Data ➤ ReportViewer from the toolbox and dropping it onto the form (see Figure 4-5 for an illustration of the process). You might also like to use the shortcut method: double-click the ReportViewer icon. If you choose to use the double-clicking method, ReportViewer will appear in the top, left corner on the Form1 design surface. Figure 4-3. Adding the ReportViewer to Form1 As a result of this, a ReportViewer control will be added to the form with the name reportViewer1. Please set the properties of the ReportViewer control per Table 4-2. We are setting the Dock property to Fill, because we want the ReportViewer to fill the entire Form1 sur face; that way, we provide the maximum amount of space to view report output. For all client-side processing of reports, we must make sure the processing mode is set to Local. Table 4-2. Properties of reportViewer1 Property Value Dock Fill ProcessingMode Local CHAPTER 4 ■ REPORTING WITH WINDOWS FORMS78 8547ch04final.qxd 8/30/07 4:08 PM Page 78 After setting up properties for the ReportViewer, your form should look like the one s hown in Figure 4-4. If it doesn’t, I’d advise you to go through the instructions again and make sure you’ve not missed anything. Figure 4-4. Final look of form design after adding the ReportViewer Another interesting observation after adding the ReportViewer control to the form is that two new assembly references are part of the project now. What is an assembly? Simply put, an assembly is a compiled code library for use while developing the needed functionality. The two assemblies, Microsoft.ReportViewer.Common and Microsoft.ReportViewer.WinForms, are used to produce the report output for viewing and exporting. You can learn more about .NET assemblies here: http://msdn2.microsoft.com/en-us/library/hk5f40ct(vs.80).aspx F igure 4-5 shows the newly added assembly references. CHAPTER 4 ■ REPORTING WITH WINDOWS FORMS 79 8547ch04final.qxd 8/30/07 4:08 PM Page 79 Figure 4-5. Two new references are added as a result of adding the ReportViewer. Adding a Dataset to the Project A dataset is very important part of developing a host client; and it should be, as it is holding the data from various sources that we’ll use to prepare the reports. Adding a dataset is easy: select the project RSWin101 in Solution Explorer; right-click it, and select Add ➤ New Item. Please see Figure 4-6 for an illustration of the steps. Figure 4-6. S teps to add a ne w item to the pr oject CHAPTER 4 ■ REPORTING WITH WINDOWS FORMS80 8547ch04final.qxd 8/30/07 4:08 PM Page 80 After completing the steps shown in Figure 4-6, you’ll be presented with the Add New Item d ialog box, where you can pick from various available templates. Please select DataSet as your choice, and name it dsRSWin101. Then, click the OK button. Figure 4-7 illustrates the steps. Figure 4-7. Steps to add a new dataset to the project After you go through all the steps mentioned in Figures 4-6 and 4-7, you’ll notice that a new dataset is added to the project and a blank dataset designer is open for you to add a data table (see Figure 4-8). Figure 4-8. D ataS et designer sur face CHAPTER 4 ■ REPORTING WITH WINDOWS FORMS 81 8547ch04final.qxd 8/30/07 4:08 PM Page 81 Building the Project That’s it—we have completed the development steps required to get our Windows Form host client ready. I’d like to remind you that there is more to Windows Forms applications; what w e did is just tip of the iceberg. However, this is what we need at minimum in order to host our report. Now, it’s the time to build the project. If you just have one project in your solution, by the way, building the solution and building the project are the same. You can build a project in a few ways: You can click the small, green play button in the main toolbox or press F5 on the keyboard to start the application in run-time mode. You can also select Build ➤ Build Solu- tion from the main menu bar of the Visual Studio IDE (see Figure 4-9). Figure 4-9. Available options for building the project If all goes well, your project should compile without any issues, and you should be able to see it in run-time mode; it should look something like Figure 4-10. You’ll notice that ReportViewer has the message “The source of the report definition has not been specified.” The reason for this mes- sage is that we haven’t bound any report to the viewer. For a quick explanation of messages like this one, see the troubleshooting section at the end of this chapter. ■ Note Building is a process in which you compile your work and produce an executable, either in debug or release mode per your choice. By default, the Visual Studio IDE is set to build in debug mode; I’d suggest tha t you stay with the default settings of the IDE, unless you want to build a release version of the solution. CHAPTER 4 ■ REPORTING WITH WINDOWS FORMS82 8547ch04final.qxd 8/30/07 4:08 PM Page 82 Figure 4-10. The form in run-time mode Using This Project As a Template You can use the RSWin101 project as a template for rest of the practical projects we’re going to do in this chapter; that is, you can copy this project to a different folder and start changing it. This way, you don’t need to create the project and add the ReportViewer, dataset, and so forth each time. From now on, I’ll focus more on showing you the steps to design the reports and coding. We’ll use same approach as this template to create the host client; if we have to put more controls on the client, I’ll show you how to add them. As you can see, in this tutorial, I have not shown you how to create the data table or add a report to the project. As both the data table and report are going to be different from project to project, you’ll see different sets of instructions according to the demands of the reporting project. ■ Note There are other ways to make use of datasets in our reporting projects. However, to be consistent, I’ll use the approach mentioned in this tutorial. This approach is more hands-on, and for every project, you’ll create a fresh dataset and related data tables from scratch. Your First Reporting Project Is on Its Way . . . Before we start the journey of learning report development, I’d like to say something here. The complexity of development efforts is different from project to project; I’ll start with a simple List Report with no data grouping or summary totals. Why such a simple report? Well, I believe it is good idea to always take a simple approach in the beginning, and later, as your confidence grows, we’ll do more complex reports. As we move through the chapters, the projects will become more and more challenging. You should also keep in mind that there is more then one way of getting a report done. You can easily think that you would have done the same reports in different ways. The mechanics I’m CHAPTER 4 ■ REPORTING WITH WINDOWS FORMS 83 8547ch04final.qxd 8/30/07 4:08 PM Page 83 selecting to do the reports in this book are chosen to use all possible functionality of client- s ide reporting. So, what are we waiting for? Let’s get the ball rolling with the Product List Reorder Point report! Product List Reorder Point Report Assume you’re working for AdventureWorks Incorporated as a developer. You have been assigned the task of developing a report that will list all the products with their respective reorder points. The report should meet all characteristics described in Table 4-3, and the report output should match Figure 4-11. Table 4-3. Report Characteristics Characteristics Value Report title Product List Reorder Point Report Company title AdventureWorks Inc. Print date Yes Data source ProductReorder Columns to report ProductNumber, Name, Color, ReorderPoint Page size Letter Page orientation Portrait Page number Yes (Page: n/n) Figure 4-11. Product List Reorder Point report output CHAPTER 4 ■ REPORTING WITH WINDOWS FORMS84 8547ch04final.qxd 8/30/07 4:08 PM Page 84 [...]... 108 CHAPTER 4 s REPORTING WITH WINDOWS FORMS Creating a Windows Forms Project Please open Visual Studio, and use the following steps, illustrated in Figure 4-1, to create a Windows application project: 1 Click File ® New ® Project, or press the hot keys Ctrl+Shift+N 2 In the “Project types” pane of the New Project dialog box, select Visual C# ® Windows 3 In the Templates pane, select Windows Application... 8547ch04final.qxd 96 8/30/07 4:08 PM Page 96 CHAPTER 4 s REPORTING WITH WINDOWS FORMS Figure 4-26 Changing properties of a report item using the Visual Studio IDE Properties window Figure 4-27 Changing Color property of text box report item using VS IDE properties window 8547ch04final.qxd 8/30/07 4:08 PM Page 97 CHAPTER 4 s REPORTING WITH WINDOWS FORMS Let’s start changing properties After selecting each... default size of the text box is 1 inch ! 0.25 inches 8547ch04final.qxd 8/30/07 4:08 PM Page 95 CHAPTER 4 s REPORTING WITH WINDOWS FORMS The text box report item is more or less the same as a standard text box control we’d use NET web forms We can display both static and dynamic text with Windows Forms or ASP using this report item Each report control that we put onto our report designer surface is an... System .Windows. Forms; System.Data.SqlClient; Microsoft .Reporting. WinForms; namespace ProductReorder { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { 103 8547ch04final.qxd 104 8/30/07 4:08 PM Page 104 CHAPTER 4 s REPORTING WITH WINDOWS FORMS //declare connection string, please substitute //DataSource with your Server name string cnString... 8/30/07 4:08 PM Page 94 CHAPTER 4 s REPORTING WITH WINDOWS FORMS Figure 4-23 Dragging report items from the toolbox to the design surface After adding the report items to the design surface, please make sure your design looks similar to my design in Figure 4-24 I dragged and dropped the report items in sequence, starting with the text box for the report title and ending with text box for the page number... CHAPTER 4 s REPORTING WITH WINDOWS FORMS Figure 4-34 Steps to access the load event of Form1 Please get rid of any default code inside the form load event, and make sure the entire code behind the Form1.cs looks like following: using using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Text; System .Windows. Forms; System.Data.SqlClient;... a Windows Forms Project Open Visual Studio, and use the following steps to create a Windows application project; please refer to Figure 4-1 from the RSWin101 exercise for an illustration of this process: 1 Click File ® New ® Project, or you can press the hot keys Ctrl+Shift+N 2 In the “Project types” pane of the New Project dialog box, select Visual C# ® Windows 3 In the Templates pane, select ® Windows. .. Figure 4-15 Column name and properties view Figure 4-16 Final look of data table inside the dataset 8547ch04final.qxd 8/30/07 4:08 PM Page 89 CHAPTER 4 s REPORTING WITH WINDOWS FORMS Step 2: Designing the Report Layout All right, we have our dataset in place with a data table and all the needed columns We’re all set to start designing the report layout, right? Wait a minute Do we have the report added to... Please make sure your report designer resembles the one shown in Figure 4-28 97 8547ch04final.qxd 98 8/30/07 4:08 PM Page 98 CHAPTER 4 s REPORTING WITH WINDOWS FORMS Figure 4-28 Report designer after dropping a table item Dropping the table item will produce a table with three rows and three columns by default You may also notice that the center column has been labeled: Header, Detail and Footer (wow,... with the report header text boxes You can do this easily: click the left-most corner of the table to select the entire table (see Figure 4-29), and use the right arrow to move to the right I’ve also set the Width properties of TableColumn1 to TableColumn4 to 1.24 inches, 3 inches, 1 inch, and 1 inch, respectively 99 8547ch04final.qxd 100 8/30/07 4:08 PM Page 100 CHAPTER 4 s REPORTING WITH WINDOWS FORMS . of reporting projects • Troubleshooting tips • Exercises for you to practice Windows Forms 101 Let’s start the journey with a quick tutorial on Windows Forms. . you with the new project. CHAPTER 4 ■ REPORTING WITH WINDOWS FORMS7 6 8547ch04final.qxd 8/30/07 4:08 PM Page 76 Figure 4-2. The Visual Studio 2005 IDE with

Ngày đăng: 05/10/2013, 08:48

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan