Printing in Silverlight 4

Another cool feature that Microsoft has included in Silverlight 4 is the ability to Print any UI Element to any printer and that too, without any complicated code. Let’s have a quick look at implementing this.

Printing starts with the PrintDocument class.  This class exposes several events – we shall use only one at this moment – that are used to call back to ask you about how to print individual pages.

Initial code:


PrintDocument docDataGrid = new PrintDocument();
docDataGrid.DocumentName = "Data Grid Print using Silverlight 4";
docDataGrid.PrintPage += new EventHandler<PrintPageEventArgs>(docDataGrid_PrintPage);
docDataGrid.Print();

We first defined a friendly DocumentName. This DocumentName is displayed in the Printer Spooler when printing starts. PrintPage is the core function that is invoked when page is printed, so formating and assigning of content should be done in this function.


void docDataGrid_PrintPage(object sender, PrintPageEventArgs e)
{

// Stretch to the size of the printed page

dataGrid1.Width = e.PrintableArea.Width;

dataGrid1.Height = e.PrintableArea.Height;

// Assign the XAML element to be printed

e.PageVisual = dataGrid1;

// Specify whether to call again for another page

e.HasMorePages = false;

}

What’s happening here? Initially we are resizing the control to be printed (here dataGrid1) to the size of PrintableArea.  This ensures that the page does not remain blank.

Then, we assign the PageVisual property to the element that needs to be printed.  If this is skipped, no content will be printed.

HasMorePages flag indicates whether there are set of trailing pages that need to be printed as well.  If this is set to true, the same method (docDataGrid_PrintPage) will be invoked again.

Continue reading » · Rating: · Written on: 01-11-10 · No Comments »

Datagrid, datalist or repeater

Developers often make a decision blindly – they choose a datagrid over datalist and repeater. But that should not be a case.

Datagrid provides ability to allow the end-user to sort, page, and edit its data. But it comes at a cost of speed. Second the display format is simple that is in row and columns.

With its templates, the DataList provides more control over the look and feel of the displayed data than the DataGrid. It offers better performance than datagrid

Repeater control allows for complete and total control. With the Repeater, the only HTML emitted are the values of the databinding statements in the templates along with the HTML markup specified in the templates—no “extra” HTML is emitted, as with the DataGrid and DataList. By requiring the developer to specify the complete generated HTML markup, the Repeater often requires the longest development time. But repeater does not provide editing features like datagrid so everything has to be coded by programmer.

Summarizing:-

Repeater does boast the best performance of the three data Web controls. Repeater is fastest followed by Datalist and finally datagrid.  Use of Repeater should be prefered over DataGrid and DataList!

Continue reading » · Rating: · Written on: 07-24-09 · No Comments »