Skip to content

Building your first report

Matt Hidinger edited this page Apr 10, 2016 · 2 revisions

Step 0 – Choose a data source for the report

In many projects you can just use your existing LINQ queries as the report's data source. These queries are great candidates to quickly turn them into DoddleReports. You can also use DataTables or even SharePoint lists, so please see Understanding the key components for more examples of IReportSource that come packaged with DoddleReport.

For this example, we will create a simple ProductRepository that generates 1000 random products.

public static class ProductRepository
{
    public static List<Product> GetAll()
    {
        var rand = new Random();
        return Enumerable.Range(1, 1000)
            .Select(i => new Product
            {
                  Id = i,
                  Name = "Product " + i,
                  Description = "Some random lines of text",
                  Price = rand.NextDouble()*100,
                  OrderCount = rand.Next(1000),
                  LastPurchase = DateTime.Now.AddDays(rand.Next(1000)),
                  UnitsInStock = rand.Next(0, 2000)
             })
            .ToList();
    }
}

Step 1 – Create a new Report() and pass it your IReportSource

DoddleReport ships with a helpful ToReportSource() extension method on IEnumerable and DataTable to easily turn the underlying data into a report source that DoddleReport can understand.

// Get the data for the report (any IEnumerable or LINQ query will work)
var query = ProductRepository.GetAll();

// Create the report and turn our query into a ReportSource
var report = new Report(query.ToReportSource());

Step 2 – Customize the report TextFields, RenderHints, and DataFields

While this step is entirely optional, it’s a good idea to fill in some basic info. Please see the Advanced Customization Page to get a better idea for report customization possibilities

// Customize the Text Fields
report.TextFields.Title = "Products Report";
report.TextFields.SubTitle = "This is a sample report showing how Doddle Report works";
report.TextFields.Footer = "Copyright 2011 &copy; The Doddle Project";
report.TextFields.Header = string.Format(@"
    Report Generated: {0}
    Total Products: {1}
    Total Orders: {2}
    Total Sales: {3:c}", DateTime.Now, totalProducts, totalOrders, totalProducts * totalOrders);


// Render hints allow you to pass additional hints to the reports as they are being rendered
report.RenderHints.BooleanCheckboxes = true;


// Customize the data fields
report.DataFields["Id"].Hidden = true;
report.DataFields["Price"].DataFormatString = "{0:c}";
report.DataFields["LastPurchase"].DataFormatString = "{0:d}";

Step 3a – Pick a Report Writer (IReportWriter)

The IReportWriter is what writes the Report to a Stream in a specific format, like Excel, HTML, or PDF.

In some cases you want to choose the ReportWriter manually (described in this step), but if you are using ASP.NET be sure to read Step 3b!

Please see Key Components for extending DoddleReport to see what report writers come packaged with DoddleReport

var writer = new HtmlReportWriter();
writer.WriteReport(report, HttpContext.Response.OutputStream);

Step 3b – Web Reporting from ASP.NET

If you are using ASP.NET be sure to read the Using DoddleReport in ASP.NET section, which describes some much simple ways to integrate DoddleReport into your ASP.NET applications