.NET DataGrid Printing

Discussion in 'OT Technology' started by dexedrine, Jul 11, 2003.

  1. dexedrine

    dexedrine Guest

    I have a DataGrid that I'm trying to print. I can print what's on the datagrid at that moment, but I have a few hundred cells that I need to print :wtc: So I looked on google, and i found an example program, however it doesn't work very well, and I tried editing it, however I can't make it do what I want. Anybody have any ideas?
     
  2. SLED

    SLED custom title

    Joined:
    Sep 20, 2001
    Messages:
    28,123
    Likes Received:
    12
    windows datagrid? or web?
     
  3. dexedrine

    dexedrine Guest

    windows...i kinda got it to work, now i'm just wondering if there's any other better ways
     
  4. SLED

    SLED custom title

    Joined:
    Sep 20, 2001
    Messages:
    28,123
    Likes Received:
    12
    personally, I would go the Crystal report way. That i know of, there is not built-in print preview for .NET

    I use Component One datagrids, and they have their own controls to do printing, and i've had really good luck with that. Their grids are x100 better too
     
  5. Scoob_13

    Scoob_13 Anything is possible, but the odds are astronomica

    Joined:
    Oct 5, 2001
    Messages:
    74,573
    Likes Received:
    453
    Location:
    Fort Worth. Hooray cowgirls.

    Crystal Reports printing is far better than the .NET printing.

    Basically, you have to use GDI + printing methodology to do some printing.

    Below is some rough (extremely!) code for printing from a C# app:

    Code:
    		private void buttonPrint_Click(object sender, System.EventArgs e)
    		{
    			PrintDialog myPrintDialog = new PrintDialog();
    			PrintDocument newDoc = new PrintDocument();
    			myPrintDialog.Document = newDoc;
    			myPrintDialog.AllowSomePages = true;
    			myPrintDialog.AllowSelection = true;
    			if(myPrintDialog.ShowDialog() == DialogResult.OK)
    			{
    				try
    				{
    					newDoc.PrintPage += new PrintPageEventHandler(this.newDoc_PrintPage);
    					newDoc.Print();
    				}
    				catch(Exception ex)
    				{
    					MessageBox.Show("There was an error printing, please retry.","Error Printing!",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
    				}
    			}
    			myPrintDialog.Dispose();			
    		}		
    
    
    			private void newDoc_PrintPage(object sender, PrintPageEventArgs e)
    		{
    			float yPos = 0;
    			float leftMargin = e.MarginBounds.Left / 2;
    			float topMargin = e.MarginBounds.Top / 2;
    			string line = null;
    			Font mainFont = new Font("Microsoft Sans Serif",8);
    			int linesPerPage = (int)(e.MarginBounds.Height / mainFont.GetHeight(e.Graphics));
    			int lineNo = pagesPrinted * linesPerPage;
    			int count = 0;
    			int upperMarginCount = 0;
    			while(count < linesPerPage && lineNo < DocDetail.Tables["DocumentDetails"].Rows.Count)
    			{
    				if(DocDetail.Tables["DocumentDetails"].Rows[lineNo]["DocID"].ToString().Length > 0 && DocDetail.Tables["DocumentDetails"].Rows[lineNo]["Description"].ToString().Length > 0)
    				{
    					if(!(DocDetail.Tables["DocumentDetails"].Rows[lineNo]["DocID"].ToString() == "RULES"))
    					{
    						line = countPrint.ToString() + "    " + DocDetail.Tables["DocumentDetails"].Rows[lineNo]["DocID"].ToString()
    							+ "     " + DocDetail.Tables["DocumentDetails"].Rows[lineNo]["Description"].ToString()
    							+ ", " + DocDetail.Tables["DocumentDetails"].Rows[lineNo]["BarCode"].ToString()
    							+ ", " + DocDetail.Tables["DocumentDetails"].Rows[lineNo]["State"].ToString();
    										line = countPrint.ToString() + "     ";
    						yPos = topMargin + (upperMarginCount * mainFont.GetHeight(e.Graphics));
    						e.Graphics.DrawString(line,mainFont,Brushes.Black,leftMargin,yPos,new StringFormat());
    						countPrint++;
    						upperMarginCount++;
    					}
    				}
    				count++;
    				lineNo++;
    			}
    	
    			
    			if(DocDetail.Tables["DocumentDetails"].Rows.Count > lineNo)
    				e.HasMorePages = true;
    			else
    				e.HasMorePages = false;
    			pagesPrinted++;
    		}
    
    
    Sorry about the structure, it's right out of some code of mine.

    There's a built in Print Preview, however you have to use the same method as printing, except instead of creating a PrintDialog object, you would create a PrintPreviewDialog (don't quote me on that, I believe that's it, either that or PrintPreview) object.

    .NET Printing sucks ass though. It's (so far, as my knowledge of C# is in it's infancy) my only complaint.

    Oh, and VB sucks :o
     

Share This Page