LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-13-2006, 08:14 AM   #1
mrobertson
Member
 
Registered: May 2005
Posts: 275

Rep: Reputation: 30
Problem Printing a Datagrid in visual C#


I am trying to print a datagrid in Visual C# and am consistently having trouble with the formatting and print size. I originally tried the following code which will only print the viewable section of a datagrid(i.e. If there are 20 columns, only the 8 or so viewable on the form will be printed)
Code:
private void btnPrint_Click(object sender, System.EventArgs e)
		{
printPreviewDialog1.Show();
			PrintDocument1.Print();
						
			
		}

		private void printDocument1_PrintPage_1(object sender, System.Drawing.Printing.PrintPageEventArgs e)
		{
			PaintEventArgs myPaintArgs = new PaintEventArgs(e.Graphics, new System.Drawing.Rectangle(new System.Drawing.Point(0, 0), dataGrid.ClientSize));
			this.InvokePaint(dataGrid, myPaintArgs);	

		}
I have also tried the code that I found off of google at C-sharpcorner.com and got a similar problem. Does anyone have any good code snippets or suggestions on a way that I can print this datagrid( all columns on multiple pages)?
 
Old 03-13-2006, 09:06 AM   #2
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
I have alos tried the code from http://www.codeproject.com/cs/miscct...ridPrinter.asp. I continually get an error on the bold faced line of the Printer class:
Code:
namespace Print
{
	/// <summary>
	/// Summary description for PrinterClass.
	/// </summary>
	public class PrinterClass
	{
		//clone of Datagrid
		private Grid PrintGrid;
		
		//printdocument for initial printer settings
		private PrintDocument PrintDoc;

		//pageSettings
		private int pageWidth;
		private int pageHeight;
		private bool pageLandscape;
		
		//pageMargins
		private int pageMarginRight;
		private int pageMarginLeft;
		private int pageMarginTop;
		private int pageMarginBottom;

		//defines whether the grid is ordered right to left
		private bool bRightToLeft;
		
		//Current Top
		private float CurrentY = 0;

		//Current Left
		private float CurrentX = 0;

		//CurrentRow to print
		private int CurrentRow = 0;

		//Page Counter
		public int PageCounter=0;

		/// <summary>
		/// Constructor Class
		/// </summary>
		/// <param name="pdocument"></param>
		/// <param name="dgrid"></param>
		public PrinterClass(PrintDocument pdocument,DataGrid dgrid)
		{
			PrintGrid = new Grid(dgrid);
			PrintDoc = pdocument;

			//The grid columns are right to left
			bRightToLeft = dgrid.RightToLeft==RightToLeft.Yes;

			//init page settings
			pageWidth = pdocument.DefaultPageSettings.PaperSize.Width;
			pageHeight = pdocument.DefaultPageSettings.PaperSize.Height;
			pageLandscape = pdocument.DefaultPageSettings.Landscape;

			//init margin settings
			pageMarginLeft = pdocument.DefaultPageSettings.Margins.Left;
			pageMarginTop = pdocument.DefaultPageSettings.Margins.Top;
			pageMarginRight = pdocument.DefaultPageSettings.Margins.Right;
			pageMarginBottom = pdocument.DefaultPageSettings.Margins.Bottom;

			//init CurrentX and CurrentY
			CurrentY = pageMarginTop;
			CurrentX =  pageMarginLeft;
			

		}

		public bool Print(Graphics g,ref float currentX,ref float currentY)
		{
			//use predefined area
			CurrentX = currentX;
			CurrentY = currentY;
			

			PrintHeaders(g);
			bool Morepages = PrintDataGrid(g);

			currentY = CurrentY;
			currentX = CurrentX;

			return Morepages;

			
		}

		public bool Print(Graphics g)
		{
			CurrentX = pageMarginLeft;
			CurrentY = pageMarginTop;

			PrintHeaders(g);
			return PrintDataGrid(g);
		}

		/// <summary>
		/// Print the Grid Headers
		/// </summary>
		/// <param name="g"></param>
		private void PrintHeaders(Graphics g)
		{
			StringFormat sf = new StringFormat();

			//if we want to print the grid right to left
			if (bRightToLeft)
			{
				CurrentX = pageWidth - pageMarginRight;
				sf.FormatFlags = StringFormatFlags.DirectionRightToLeft;
			}
			else
			{
				CurrentX = pageMarginLeft;
			}
			MessageBox.Show(PrintGrid.Columns.ToString());
			for (int i=0;i<PrintGrid.Columns;i++)
			{
				//set header alignment
				switch (((Header)PrintGrid.Headers.GetValue(i)).Alignment)
				{
						//left
					case HorizontalAlignment.Left:
						sf.Alignment = StringAlignment.Near;
						break;

						//right
					case HorizontalAlignment.Center:
						sf.Alignment = StringAlignment.Center;
						break;

						//right
					case HorizontalAlignment.Right:
						sf.Alignment = StringAlignment.Far;
						break;
				}

				//advance X according to order
				if (bRightToLeft)
				{
						
					//draw the cell bounds (lines) and back color
					g.FillRectangle(new SolidBrush(PrintGrid.HeaderBackColor),CurrentX - PrintGrid.Headers[i].Width,CurrentY,PrintGrid.Headers[i].Width,PrintGrid.Headers[i].Location.Height);
					g.DrawRectangle(new Pen(PrintGrid.LineColor),CurrentX - PrintGrid.Headers[i].Width,CurrentY,PrintGrid.Headers[i].Width,PrintGrid.Headers[i].Location.Height);
					
					
					//draw the cell text
					g.DrawString(PrintGrid.Headers[i].Text,PrintGrid.Headers[i].Font,new SolidBrush(PrintGrid.HeaderForeColor),new RectangleF(CurrentX - PrintGrid.Headers[i].Width,CurrentY,PrintGrid.Headers[i].Width,PrintGrid.Headers[i].Height),sf);
			
					//next cell
					CurrentX -=PrintGrid.Headers[i].Width;
					
				}
				else
				{
						
					//draw the cell bounds (lines) and back color
					g.FillRectangle(new SolidBrush(PrintGrid.HeaderBackColor),CurrentX,CurrentY,PrintGrid.Headers[i].Width,PrintGrid.Headers[i].Location.Height);
					g.DrawRectangle(new Pen(PrintGrid.LineColor),CurrentX,CurrentY,PrintGrid.Headers[i].Width,PrintGrid.Headers[i].Location.Height);
					
					
					//draw the cell text
					g.DrawString(PrintGrid.Headers[i].Text,PrintGrid.Headers[i].Font,new SolidBrush(PrintGrid.HeaderForeColor),new RectangleF(CurrentX,CurrentY,PrintGrid.Headers[i].Width,PrintGrid.Headers[i].Height),sf);
					
					//next cell
					CurrentX +=PrintGrid.Headers[i].Width;
				}
					
					
			}

			//reset to beginning
			if (bRightToLeft)
			{
				//right align
				CurrentX = pageWidth - pageMarginRight;
			}
			else
			{
				//left align
				CurrentX = pageMarginLeft;
			}

			//advance to next row
			CurrentY = CurrentY + ((Header)(PrintGrid.Headers.GetValue(0))).Location.Height;			 
		}

		private bool PrintDataGrid(Graphics g)
		{	
			StringFormat sf = new StringFormat();
			PageCounter++;
			
			//if we want to print the grid right to left
			if (bRightToLeft)
			{
				CurrentX = pageWidth - pageMarginRight;
				sf.FormatFlags = StringFormatFlags.DirectionRightToLeft;
			}
			else
			{
				CurrentX = pageMarginLeft;
			}

			for (int i=CurrentRow;i<PrintGrid.Rows;i++)
			{
				for (int j=0;j<PrintGrid.Columns;j++)
				{
					//set cell alignment
					switch (PrintGrid[i,j].Alignment)
					{
							//left
						case HorizontalAlignment.Left:
							sf.Alignment = StringAlignment.Near;
							break;

							//center
						case HorizontalAlignment.Center:
							sf.Alignment = StringAlignment.Center;
							break;

							//right
						case HorizontalAlignment.Right:
							sf.Alignment = StringAlignment.Far;
							break;
					}
					
					//advance X according to order
					if (bRightToLeft)
					{
						
						//draw the cell bounds (lines) and back color
						g.FillRectangle(new SolidBrush(PrintGrid.BackColor),CurrentX - PrintGrid[i,j].Width,CurrentY,PrintGrid[i,j].Width,PrintGrid[i,j].Height);
						g.DrawRectangle(new Pen(PrintGrid.LineColor),CurrentX - PrintGrid[i,j].Width,CurrentY,PrintGrid[i,j].Width,PrintGrid[i,j].Height);
						
					
						//draw the cell text
						g.DrawString(PrintGrid[i,j].Text,PrintGrid[i,j].Font,new SolidBrush(PrintGrid.ForeColor),new RectangleF(CurrentX - PrintGrid[i,j].Width,CurrentY,PrintGrid[i,j].Width,PrintGrid[i,j].Height),sf);

						//next cell
						CurrentX -=PrintGrid[i,j].Width;
					
					}
					else
					{
						
						//draw the cell bounds (lines) and back color
						g.FillRectangle(new SolidBrush(PrintGrid.BackColor) ,CurrentX,CurrentY,PrintGrid[i,j].Width,PrintGrid[i,j].Height);
						g.DrawRectangle(new Pen(PrintGrid.LineColor) ,CurrentX,CurrentY,PrintGrid[i,j].Width,PrintGrid[i,j].Height);
						
					
						//draw the cell text
						//Draw text by alignment
						
							
						g.DrawString(PrintGrid[i,j].Text,PrintGrid[i,j].Font,new SolidBrush(PrintGrid.ForeColor),new RectangleF(CurrentX,CurrentY,PrintGrid[i,j].Width,PrintGrid[i,j].Height),sf);
							
						//next cell
						CurrentX +=PrintGrid[i,j].Width;
					}
					
					
				}

				//reset to beginning
				if (bRightToLeft)
				{
					//right align
					CurrentX = pageWidth - pageMarginRight;
				}
				else
				{
					//left align
					CurrentX = pageMarginLeft;
				}
				
				//advance to next row
                CurrentY = CurrentY + PrintGrid[i,0].Height;
				CurrentRow++;
				//if we are beyond the page margin (bottom/side) then we need another page,
				//return true
				
				//if portrait is selected
				if((CurrentY > pageHeight - 
					pageMarginBottom)&&(!pageLandscape))
					{
					return true;
					}

				//if landscape is selected
				if((CurrentY > pageWidth - 
					pageMarginRight)&&(pageLandscape))
					{
					return true;
					}
				
			}
			CurrentRow = 0;
			return false;
			
		}

	}
}
The error states that the objest was not set to reference and instance. Does anyone see the problems here?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Stupid Question: Microsoft Visual Studio 6.0 (A.K.A. Visual Basic 6) Will it run? S0RD3N Linux - Software 8 05-07-2008 09:42 PM
Printing a datagrid/dataset in c# mrobertson Programming 1 02-27-2006 04:43 PM
Mono and Datagrid Vagrant Programming 1 02-23-2006 01:35 AM
running Visual Basic/Visual Fox Pro in Linux? depam Linux - Software 9 02-11-2006 04:42 PM
Visual Basic printing Joe Soap Programming 2 07-17-2002 01:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration