Reporting Services - Matrix Control

Posted on March 6, 2008 by Chris at 10:37 am

When writing reports for my various work projects, I’ve always tried to work as fast as possible, and tend to ignore the things that don’t make sense strait off the bat. These things tend to take a lot more time to learn and figure out. I never really have a lot of time, so I find another way that is faster at the time.

One of the things that I never got to play with was the Matrix control in the Microsoft reporting services. It seemed logical enough, but every time I looked at using it, the damed thing never worked, so I went and used the List control instead. This time, I was writing a report that needed to have a different number of columns depending on the number weeks in the month being reported on. So, naturally, I thought that the Matrix control would work well.

Here’s an example of what the data that I started with looks like. I was able to pull this out of my tables with a (relatively) simple sql statement.

Next I threw the Matrix control on a new report, and using the report printer that I prepared earlier in Microsoft SQL Server Reporting Sevices, I got a blank report printing out to pdf (yay!).

After a little trial and error, which I had time for this time (accounts REALLY wants this report), I finally found the Groups tab on the Properties page of the Matrix control. This was the key to getting it to work correctly. After that, it was clear sailing.

As you can see, I’ve added two Row Groups, and 1 Column group. The row groups determine what is shown in each row, while the column groups determine how what is shown in the columns. I know, it sounds like I’m doing the dummies guide, but really I do remember thinking that exact line while actually doing the work, followed by “ooh, so that’s how it works”.. guess I really am a dummy.

Anyhoo, this is what the relevant part of the group edit screen looks like. Incedently, the row groups were on section name, then staff name.

Then I added the field values into the grid items that were available, and after a little trial and error, came out with this tiny, little thing.

Although it’s tiny, it has big results.. take a look.. ooh pretty…

Data Binding in Windows Forms

Posted on January 10, 2008 by Chris at 12:37 pm

Was playing with an old application that I wrote for work and realised that I’ve completely forgotten about how data bindings work with Windows Forms applicatios. So, I thought I’d add a blog entry about it, so that I can refer back to my various pearls later.

Data binding is a nifty method of making your controls talk directly to your data. While there are other methods that may be faster, data binding is very useful for getting things up and running quickly.

All System.Windows.Forms.Control objects come with their own DataBindings member. This is the thing that allows the magic to happen.

Suppose that you had a DataTable that you were storing all of you application’s data in, and this table contained the columns customer_id, customer_name, customer_address, customer_type and so on.

To set up a databinding to these items you would use code similar to below.

   // m_CustomerName is a TextBox added using the form designer
   // m_DataRow is the record that you are currently editing
   m_CustomerName.DataBindings.Add("Text", m_DataRow, "customer_name");

Viola, now whenever you make a change in the text box, your record will be updated as well, and as easy as adding a single line of code to your initialization function. As an added bonus, your textbox will have it’s value set to whatever is in the field when you bind it as well.

Suppose that you have a specific type that you would prefer to fill out, like the one below.

   public class Customer
   {
      public int CustomerID {get; set;}
      public String CustomerName {get; set;}
      public String CustomerAddress {get; set;}
      public int CustomerType {get; set;}
    }

The code for doing this would be exactly the same, with the property name passed as the paramater.

   m_CustomerName.DataBindings.Add("Text", m_CustomerStruct, "CustomerName");

Microsoft SQL Server Reporting Sevices

Posted on December 11, 2007 by Chris at 8:45 am

I find myself doing alot of web based reporting for the current project that I am doing at the moment, which is no surprise, as the application that I am writing is basically a data-mining tool. One thing that has saved me alot of time is the reporting tools that come packaged with Visual Studio 8.

I like the way that the reporting services allows you to simply add a control to your web page, and suddenly, you can view the report on your web page. But what if you don’t want to show the form, or have something that is easier to print out, the ReportViewer control is just a little too tricky to use for most of my users. Fortunately, there is a way to print a PDF or Excel spreadsheet directly, without having to have the control placed on your page.

This method used the ReportViewer.LocalReport.Render() method, which simply draws the report, and returns you the bytes for whichever format that you choose. Here’s an example of how to draw to PDF:

//Includes
using System.Data;
using System.Collections.Generic;
using Microsoft.Reporting.WebForms;

public void GenerateReport(
   String reportPath,
   List paramaters,
   String dataSourceName,
   DataTable reportData,
   String fileName)
{
   // Definitions-a-plenty
   string mimeType, encoding, extension;
   Warning[] warnings;
   string[] streamids;
   byte[] bytes;

   ReportViewer reportViewer = new ReportViewer();

   // Add in the report data
   reportViewer.LocalReport.ReportPath = reportPath;
   reportViewer.LocalReport.SetParameters(paramaters);
   reportViewer.LocalReport.DataSources.Add(new ReportDataSource(dataSourceName, reportData));
   reportViewer.LocalReport.Refresh();

   // Render the report to PDF
   bytes = reportViewer.LocalReport.Render(
      "PDF",
      null,
      out mimeType,
      out encoding,
      out extension,
      out streamids,
      out warnings);

   // Finally, write the bytes directly to the output
   // stream
   //
   // This should cause your browser to recognise a PDF
   // coming out, rather than a web page
   //
   // NOTE: Will not work correctly if you have already
   //       written to the output stream, as it does
   //       things with page headers
   Response.AddHeader("Content-Disposition", "attachment; filename="+fileName+extension);
   Response.ContentType = mimeType;
   Response.OutputStream.Write(bytes, 0, bytes.Length);
   Response.End();
}