ColaReport - Demo 4
 Home
 FAQ
 Examples
 Documentation
 Download
 Feedback
 Support
  demo4.aspx.cs       Return to Demo 4       Return to Examples

using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using ColaReport;

namespace ColaReportDemo
{
	/// <summary>
	/// Demonstrates:
	/// 1. Using pregroup labels.
	/// 2. Displaying groups when the group value is not in a column.
	/// 3. Allowing the user to output to Excel.
	/// </summary>
	public class demo4 : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Label lblReport;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			Report oReport;
			CellColumn oCol;
			Group oGroup;
			DataSet dsRpt;
			string sQuery;

			OleDbConnection oConn;
			OleDbDataAdapter daRpt;

			// Get connection string
			oConn = new OleDbConnection (ConfigurationSettings.AppSettings["gsConn"]);
			
			//We later create a "group" on CustomerName.
			//It is therefore important to order by CustomerName here.
			sQuery = "SELECT c.CustomerName, i.InvoiceDate, i.InvoiceId, i.InvoiceTotal" +
				" FROM Customer c, Invoice i" +
				" WHERE c.customerid = i.customerid" +
				" ORDER BY c.CustomerName, i.InvoiceDate, i.InvoiceId";

			daRpt = new OleDbDataAdapter (sQuery, oConn);
			dsRpt = new DataSet();
			daRpt.Fill(dsRpt);

			// Define Report 
			oReport = new Report ();

			// See if we should output to Excel
			if (Request["toExcel"] == "1")
			{ 
				// Change content type, removing page breaks
				Response.ContentType = "application/vnd.ms-excel";
				Response.Charset = "";
				this.EnableViewState = false;
				
				// IMPORTANT! Turn off padding for Excel numeric data
				oReport.PadCellColumns = false;
								
				oReport.LinesPerPage = 0;
				oReport.LinesFirstPage = 0;
			} 
			else
			{
				oReport.LinesFirstPage = 33;
				oReport.LinesPerPage = 33;
			}


			oReport.Style.Add ("font-family", "Verdana");
			oReport.HeaderRowAttributes.Attributes["bgcolor"] = "#efefdf";

			oReport.FirstPageHeader.Text = "<h2<Demo: demo4.aspx>/h2>";
			oReport.FirstPageHeader.Style.Add ("font-family", "Verdana");
			oReport.PageHeader.Text = "<h3<Demo: demo4.aspx>/h3>";
			oReport.PageHeader.Style.Add ("font-family", "Verdana");
	
			oCol = oReport.CreateCellColumn ("InvoiceDate", "Invoice Date"); 
			oCol.StringFormat = "{0:d}";  //date format

			oCol = oReport.CreateCellColumn ("InvoiceId", "Invoice Number"); 
			oCol.Attributes["align"] = "right";

			oCol = oReport.CreateCellColumn ("InvoiceTotal", "Invoice Total"); 
			oCol.SumColumn = true; //display totals for this column
			oCol.Attributes["align"] = "right";
			oCol.StringFormat = "{0:c}";  //currency format


			// Create a group for the grand totals
			oGroup = oReport.CreateTotalGroup (true);
			oGroup.Cells["InvoiceDate"].Text = "Total";	
			oGroup.Cells["InvoiceDate"].StringFormat = ""; //reset; it inherited "{0:d}"
			oGroup.Cells["InvoiceDate"].Style.Add("font-weight", "bold");
			oGroup.Cells["InvoiceTotal"].Style.Add("font-weight", "bold");
			oGroup.BackColor = System.Drawing.Color.FromArgb (249,249,200);
			oGroup.BlankLineAfterGroup = false;
			 
			// Group data by CustomerName
			oGroup = oReport.CreateGroup ("CustomerName", true);

			// Note, there is no CellColumn defined for CustomerName.
			// We put the Customer in the InvoiceDate column for the group.
			oGroup.Cells["InvoiceDate"].StringFormat = "{0:g} Subtotal";
			oGroup.Cells["InvoiceDate"].Style.Add("font-weight", "bold");

			// Indicate that we want a line before the group begins
			oGroup.PreGroupLabel = true;
			oGroup.PreGroupRow.Attributes["bgcolor"] = "#ccffff";
			oGroup.PreGroupRow.Cells["InvoiceDate"].StringFormat = "<b>Customer: {0:g}</b>";

			oGroup.Attributes["bgcolor"] = "#efefef";
			//oGroup.BackColor = System.Drawing.Color.FromArgb (249,249,249);

			//Bind dataset
			oReport.Bind (dsRpt);

			//Set label text to report output
			lblReport.Text = oReport.Execute();

			// If we did not output to Excel, output a link allowing that option.
			if (!(Request["toExcel"] == "1"))
			{
				lblReport.Text += "<a href='" + Request.RawUrl + "?toExcel=1'>Send To Excel</a>";
			}
		}
	}
}
      
Copyright © 2003