none
Creating a simple reporting component in c# RRS feed

  • General discussion

  • Hi,

    I've finished converting my old Delphi Windows application to C# but I have a request for guidance.  The old application uses a tool called Rave Reports to generate a reports entirely in code, based on the contents of the custom, in-memory object that drives the application.  Key data for that class is saved to a DB but many of the properties are code based an data is only generated at when needed.  I've managed to reproduce the Rave Reports functionality in the C# version using the System.Drawing.Printing.PrintDocument and the System.Drawing.Printing.PrintPreviewDialog components (instantiated in code).

    I'm quite pleased with the results I've achieved but now I'd like to extract that functionality to a reporting component that can be used in other applications that require simple, text reporting without having to recreate all the necessary logic each time.  Ideally, I'd like to be able to drop a component on a Windows form or instantiate a report in code then call an report method that gives me complete control over the reporting process by setting public properties and calling public methods that allow me to supply data to be printed, position text (both preview and actual printing) and keep up with things like page numbers, etc.  As I said, I've got all this working just fine in my current application but the use of the PrintDocument.PrintPage event seems very constricting when trying to use it in a reporting component that I can easily reuse in other applications.

    Do I need to abandon the provided printing components and create a new component that has its own Graphics object?  Are there any examples out there that try to do the same thing?  I'm mostly retired these days so this is exclusively for my own use and a learning exercise as much as anything else.

    Thanks, 

    Ray

    Tuesday, February 26, 2019 5:05 PM

All replies

  • If the data is in memory then use the built in LocalReport that is part of SQL Reporting. This is part of the framework and, while it comes from SQL, doesn't require SQL Server or SSRS. It can be used against any data in memory. It requires a little more work than a true reporting engine but it is free. If you have a web or Winforms app then it provides built in controls to host the report otherwise you can generate a PDF and then display it using the PDF viewer that is installed.

    The reports themselves are written in RDLC which Visual Studio can provide a graphical designer for. They are basically the same as SSRS RDL files but with the C added for client. You can build arbitrarily complex reports with them including charting and nested reports. Again, the hard part is setting up all the data to support them.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, February 26, 2019 7:12 PM
    Moderator
  • Thanks, Michael.  I've done that before, creating a data set in memory then populating it with data from a Windows form and setting the report's data source manually.  Like I said, this is largely a learning exercise for myself.

    Ray

    Tuesday, February 26, 2019 9:50 PM
  • Hi Ray

    1. Add Typed DataSet to the Project
    Since I am using disconnected RDLC Reports we will make use of Typed DataSet to populate the RDLC Reports with data from database.
    2. Adding DataTable to the Typed DataSet
    Our next step would be to add a DataTable to the Type DataSet.
    3. Adding Columns or fields to DataTable
    In the DataTable we need to specify the column names that we want to display in the RDLC Report.
    4. Adding the RDLC Report
    Using the Add New Item option in Visual Studio you need to add new RDLC Report. I am making use of Report Wizard so that it make easier to configure the Report.
    5. Choose the DataSet
    Now we need to choose the DataSet that will act as the DataSource for the RDLC Report. Thus we need to select the Customers DataSet that we have created earlier.
    6. Choose the Fields to be displayed in the RDLC Report
    Next we need to choose the fields that we need to display, we need to simply drag and drop each fields into the Values Box as shown in the screenshot below.
    7. Choose the Layout
    The next dialog will ask us to choose the layout, we can simply skip it as of now as this is a simple Report with no calculations involved.
    8. Choose the Style
    Finally we need to choose the style, i.e. color and theme of the Report.
    9. Adding Report Viewer to the Form
    In order to display the Report we will need to add ReportViewer control to the Form from the Toolbox.
    10. Populating the RDLC Report from Database
    Below is the code to populate the RDLC Report from database. The Customers DataSet is populated with records from the Customers Table and is set as ReportSource to the Report.
    //-------------

    using System.Data;
    using System.Data.SqlClient;
    using Microsoft.Reporting.WinForms;
     private void Form1_Load(object sender, EventArgs e)
    {
        Customers dsCustomers = GetData();
        ReportDataSource datasource = new ReportDataSource("Customers", dsCustomers.Tables[0]);
        this.reportViewer1.LocalReport.DataSources.Clear();
        this.reportViewer1.LocalReport.DataSources.Add(datasource);
        this.reportViewer1.RefreshReport();
    }
    private Customers GetData()
    {
        string constr = @"Data Source=.\Sql2005;Initial Catalog=Northwind;Integrated Security = true";
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT TOP 20 * FROM customers"))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (Customers dsCustomers = new Customers())
                    {
                        sda.Fill(dsCustomers, "DataTable1");
                        return dsCustomers;
                    }
                }
            }
        }
    }

    Best Regards.

    Wednesday, February 27, 2019 9:54 AM
  • Thanks.  Yes, I know how to do that and have done it before.  What I'm trying to get at is a low-level reporting solution that avoids SSRS and RDLC completely. Again, this is mostly as a learning exercise for myself.  I've already got this working with a System.Drawing.Printing.PrintDocument and System.Drawing.Printing.PrintPreviewDialog in the little project I'm working on now but I'd like to get more flexibility than those components seem to offer with complete control over the printing process.

    I'm not really looking for the quickest, easiest way to do it.  I would use the solution you suggest if I were writing this for someone else.  This is entirely for my own elucidation and entertainment.

    Thanks again,

    Ray

    Wednesday, February 27, 2019 4:24 PM