|
|
|
|
|
|
Author Rank:
|
|
Technologies:
Printing,Visual C# .NET
|
|
Total downloads :
|
4691
|
|
Total page views :
|
177506
|
|
Rating :
|
|
1.83/5
|
|
This article has been rated :
|
6 times
|
|
|
|
|
Download
Files:
|
|
|
|
|
|
|
|
|
|
Similar ArticlesMost ReadTop RatedLatest
|
|
Related EbooksTop Videos
|
|
|
Description
|
|
The Complete Visual C# Programmer's Guide, written by the authors of C# Corner, covers most of the major components that make up C# and the .NETenvironment including Windows Forms, ADO.NET, GDI+, Web Services, and Security. The book is geared toward the beginner to intermediate programmers.
|
|
|
|
|
|
|
|
|
|
|
|
|
The DataGrid is a highly versatile component of the .NET architecture and probably one of the most complex components. I wrote this article in response to the question, "How the heck do I print out a DataGrid and its contents". My first off the cuff suggestion was to capture the form using my screen capture article, but this of course does not solve the problem of printing out the umpteen rows being virtually displayed in the DataGrid. Then I thought to myself, this should be easy, I'll just use GDI+ and go through the rows in the DataGrid and print out its contents. Well the DataGrid is a bit more complex than that because it does not contain the data within itself. The data is contained within the DataSet. So the approach I settled on was to capture the color and font properties from the DataGrid for the print out, and the capture the information in the rows from the DataSet. In order to encapsulate the drawing of the DataGridPrinter to the Printer, I created the DataGridPrinter class shown in Figure 2 below. This class takes a DataGrid, a PrintDocument, and a DataTable passed to its constructor and utilizes these objects to draw the DataGrid to the printer.

Figure 1. The Print Preview of the Northwind DataGrid

Figure 2. DataGridPrinter Class UML Design (Reverse engineered using WithClass 2000)
The DataGridPrinter is constructed in the constructor of the form so it can be utilized by all of the printing functions (print, print preview, etc.) Below is the code for constructing the DataGridPrinter:
void SetupGridPrinter() { dataGridPrinter1 = new DataGridPrinter(dataGrid1, printDocument1, dataSet11.Customers); }
Once the DataGridPrinter is constructed, you can have it draw the DataGrid to the printer by calling its DrawDataGrid method in the Print Page event handler:
private void printDocument1_PrintPage(object sender,System.Drawing.Printing.PrintPageEventArgs e) { Graphics g = e.Graphics; // Draw a label title for the grid DrawTopLabel(g); // draw the datagrid using the DrawDataGrid method passing the Graphics surface bool more = dataGridPrinter1.DrawDataGrid(g); // if there are more pages, set the flag to cause the form to trigger another print page event if (more == true) { e.HasMorePages = true; dataGridPrinter1.PageNumber++; } }
The PrintPage event is triggered by both the Print method in the PrintDocument and the PrintPreviewDialog's ShowDialog method. Below is the form's method for printing the DataGrid to the printer:
private void PrintMenu_Click(object sender, System.EventArgs e) { // Initialize the datagrid page and row properties dataGridPrinter1.PageNumber = 1; dataGridPrinter1.RowCount = 0; // Show the Print Dialog to set properties and print the document after ok is pressed. if (printDialog1.ShowDialog() == DialogResult.OK) { printDocument1.Print(); } }
Now let's take a look at the internals of the DataGridPrinter methods. There are two main methods in the DataGridPrinter class that do all the drawing: DrawHeader and DrawRows. Both these methods extract information from the DataGrid and the DataTable to draw the DataGrid. Below is the method for drawing the rows of the DataGrid:
public bool DrawRows(Graphics g) { try { int lastRowBottom = TopMargin; // Create an array to save the horizontal positions for drawing horizontal gridlines ArrayList Lines = new ArrayList(); // form brushes based on the color properties of the DataGrid // These brushes will be used to draw the grid borders and cells SolidBrush ForeBrush = new SolidBrush(TheDataGrid.ForeColor); SolidBrush BackBrush = new SolidBrush(TheDataGrid.BackColor); SolidBrush AlternatingBackBrush = new SolidBrush TheDataGrid.AlternatingBackColor); Pen TheLinePen = new Pen(TheDataGrid.GridLineColor, 1); // Create a format for the cell so that the string in the cell is cut off at the end of the column width StringFormat cellformat = new StringFormat(); cellformat.Trimming = StringTrimming.EllipsisCharacter; cellformat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit; // calculate the column width based on the width of the printed page and the # of columns in the DataTable // Note: Column Widths can be made variable in a future program by playing with the GridColumnStyles of the // DataGrid int columnwidth = PageWidth/TheTable.Columns.Count; // set the initial row count, this will start at 0 for the first page, and be a different value for the 2nd, 3rd, 4th, etc. // pages. int initialRowCount = RowCount; RectangleF RowBounds = new RectangleF(0, 0, 0, 0); // draw the rows of the table for (int i = initialRowCount; i < TheTable.Rows.Count; i++) { // get the next DataRow in the DataTable DataRow dr = TheTable.Rows[i]; int startxposition = TheDataGrid.Location.X; // Calculate the row boundary based on teh RowCount and offsets into the page RowBounds.X = TheDataGrid.Location.X;RowBounds.Y = TheDataGrid.Location.Y + TopMargin + ((RowCount - initialRowCount)+1) * (TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway); RowBounds.Height = TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway; RowBounds.Width = PageWidth; // save the vertical row positions for drawing grid lines Lines.Add(RowBounds.Bottom); // paint rows differently for alternate row colors if (i%2 == 0) { g.FillRectangle(BackBrush, RowBounds); } else { g.FillRectangle(AlternatingBackBrush, RowBounds); } // Go through each column in the row and draw the information from the DataRowfor (int j = 0; j < TheTable.Columns.Count; j++) { RectangleF cellbounds = new RectangleF(startxposition, TheDataGrid.Location.Y + TopMargin + ((RowCount - initialRowCount) + 1) * (TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway), columnwidth, TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway); / draw the data at the next position in the row if (startxposition + columnwidth <= PageWidth) { g.DrawString(dr[j].ToString(), TheDataGrid.Font, ForeBrush, cellbounds,cellformat); lastRowBottom = (int)cellbounds.Bottom; } // increment the column position startxposition = startxposition + columnwidth; } RowCount++; // when we've reached the bottom of the page, draw the horizontal and vertical grid lines and return true if (RowCount * (TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway) > PageHeight * PageNumber) - (BottomMargin+TopMargin)) { DrawHorizontalLines(g, Lines);DrawVerticalGridLines(g, TheLinePen, columnwidth, lastRowBottom); return true; } } // when we've reached the end of the table, draw the horizontal and vertical grid lines and return false DrawHorizontalLines(g, Lines); DrawVerticalGridLines(g, TheLinePen, columnwidth, lastRowBottom); return false; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); return false; } }
The method goes through each row in the DataTable and draws the data. The method uses the properties of the DataGrid to paint each row with the appropriate colors and draw each string with the DataGrid's font. If the method reaches the bottom of the page, it breaks and returns true so that the rest of the DataGrid can be printed on the following page.
Improvements:
This class can be greatly improved by utilizing the DataGridColumnStyle class stored in the TableStyles property of the DataGrid. These properties allow you to specify different column width's for certain columns and different text alignments.
|
|
|
Login
to add your contents and source code to this article
|
|
|
|
|
|
|
|
|
|
|
|
Mike Gold
Michael Gold is President of Microgold Software Inc., makers of the WithClass UML Tool. His company is a Microsoft VBA Partner and Borland Partner. Mike is a Microsoft MVP and founding member of C# Corner. He has a BSEE and MEng EE from Cornell University and has consulted for Chase Manhattan Bank, JP Morgan, Merrill Lynch, and Charles Schwab. Currently he is a senior developer at Finisar Corp. He has been involved in several .NET book projects, and is currently working on a book for using .NET with embedded systems. He can be reached at mike@c-sharpcorner.com
|
|
|
|
|
|
|
|
|
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional
consulting company, our consultants are well-known experts in .NET and many of them
are MVPs, authors, and trainers. We specialize in Microsoft .NET development and
utilize Agile Development and Extreme Programming practices to provide fast pace
quick turnaround results. Our software development model is a mix of Agile Development,
traditional SDLC, and Waterfall models.
|
|
Click here to learn more about C# Consulting. |
|
|
|
|
|
|
|
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon.
Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees.
As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
|
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
|
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
|
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today. With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications. Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
|
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or
application via a range of API's. Learn More about our API connections.
|
Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 offers more to developers than any other
Visual Studio release. Work more productively and collaboratively-with
greater control over your work at every step. The Beta 2 can give you a
head start on achieving efficiency.
|
|
|
|
|
|
|
|
|
Download
Files:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|