473,385 Members | 1,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Gridview data's is not printing,Getting empty sheet out....

Hi everyone,

I'm trying to take print out from C#(winforms) Gridview,when I give print I'm getting empty sheet out not printing data's on the paper and not getting any error too..Can anyone tell me whats wrong with my code??

Here is my code:

Expand|Select|Wrap|Line Numbers
  1. namespace PrintandPrintPreview
  2. {
  3.     public partial class Form1 : Form
  4.     {
  5.         #region Member Variables
  6.         StringFormat strFormat; //Used to format the grid rows.
  7.         ArrayList arrColumnLefts = new ArrayList();//Used to save left coordinates of columns
  8.         ArrayList arrColumnWidths = new ArrayList();//Used to save column widths
  9.         int iCellHeight = 0; //Used to get/set the datagridview cell height
  10.         int iTotalWidth = 0; //
  11.         int iRow = 0;//Used as counter
  12.         bool bFirstPage = true; //Used to check whether we are printing first page
  13.         bool bNewPage = false;// Used to check whether we are printing a new page
  14.         int iHeaderHeight = 0; //Used for the header height
  15.         #endregion
  16.  
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.             printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
  21.         }
  22.  
  23.  private void Form1_Load(object sender, EventArgs e)
  24.         {
  25.             // TODO: This line of code loads data into the 'rajiDataSet.orders' table. You can move, or remove it, as needed.
  26.             this.ordersTableAdapter.Fill(this.rajiDataSet.orders);
  27.  
  28.  
  29.         }
  30.  
  31.         private void btnprint_Click(object sender, EventArgs e)
  32.         {
  33.             //Open the print dialog
  34.             PrintDialog printDialog = new PrintDialog();
  35.             printDialog.Document = printDocument1; ;
  36.             printDialog.UseEXDialog = true;
  37.             //Get the document
  38.             if (DialogResult.OK == printDialog.ShowDialog())
  39.             {
  40.                 printDocument1.DocumentName = "Document is printing";
  41.                printDocument1.Print();
  42.             }
  43.             /*
  44.             Note: In case you want to show the Print Preview Dialog instead of 
  45.             Print Dialog then comment the above code and uncomment the following code
  46.             */
  47.  
  48.              //Open the print preview dialog
  49.              //PrintPreviewDialog objPPdialog = new PrintPreviewDialog();
  50.              //objPPdialog.Document = printDocument1;
  51.              //objPPdialog.ShowDialog();
  52.         }
  53.  
  54.         private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
  55.         {
  56.             try
  57.             {
  58.                 //Set the left margin
  59.                 int iLeftMargin = 0;
  60.                 if (e.MarginBounds.Left > 0)
  61.                 {
  62.                     iLeftMargin = e.MarginBounds.Left;
  63.                 }
  64.                 //Set the top margin
  65.                 int iTopMargin = 0;
  66.                 if (e.MarginBounds.Top > 0)
  67.                 {
  68.                     iTopMargin = e.MarginBounds.Top;
  69.                 }
  70.                 //Whether more pages have to print or not
  71.                 bool bMorePagesToPrint = false;
  72.                 int iTmpWidth = 0;
  73.  
  74.                 //For the first page to print set the cell width and header height
  75.                 if (bFirstPage)
  76.                 {
  77.                     foreach (DataGridViewColumn GridCol in dataGridView1.Columns)
  78.                     {
  79.                         iTmpWidth = (int)(Math.Floor((double)((double)GridCol.Width /
  80.                             (double)iTotalWidth * (double)iTotalWidth *
  81.                             ((double)e.MarginBounds.Width / (double)iTotalWidth))));
  82.  
  83.                         iHeaderHeight = (int)(e.Graphics.MeasureString(GridCol.HeaderText,
  84.                             GridCol.InheritedStyle.Font, iTmpWidth).Height) + 11;
  85.  
  86.                         // Save width and height of headers
  87.                         arrColumnLefts.Add(iLeftMargin);
  88.                         arrColumnWidths.Add(iTmpWidth);
  89.                         iLeftMargin += iTmpWidth;
  90.                     }
  91.                 }
  92.                 //Loop till all the grid rows not get printed
  93.                 while (iRow <= dataGridView1.Rows.Count - 1)
  94.                 {
  95.                     DataGridViewRow GridRow = dataGridView1.Rows[iRow];
  96.                     //Set the cell height
  97.                     iCellHeight = GridRow.Height + 5;
  98.                     int iCount = 0;
  99.                     //Check whether the current page settings allows more rows to print
  100.                     if (iTopMargin + iCellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
  101.                     {
  102.                         bNewPage = true;
  103.                         bFirstPage = false;
  104.                         bMorePagesToPrint = true;
  105.                         break;
  106.                     }
  107.                     else
  108.                     {
  109.                         if (bNewPage)
  110.                         {
  111.                             //Draw Header
  112.                             e.Graphics.DrawString("Customer Summary",
  113.                                 new Font(dataGridView1.Font, FontStyle.Bold),
  114.                                 Brushes.Black, e.MarginBounds.Left,
  115.                                 e.MarginBounds.Top - e.Graphics.MeasureString("Customer Summary",
  116.                                 new Font(dataGridView1.Font, FontStyle.Bold),
  117.                                 e.MarginBounds.Width).Height - 13);
  118.  
  119.                             String strDate = DateTime.Now.ToLongDateString() + " " +
  120.                                 DateTime.Now.ToShortTimeString();
  121.                             //Draw Date
  122.                             e.Graphics.DrawString(strDate,
  123.                                 new Font(dataGridView1.Font, FontStyle.Bold), Brushes.Black,
  124.                                 e.MarginBounds.Left +
  125.                                 (e.MarginBounds.Width - e.Graphics.MeasureString(strDate,
  126.                                 new Font(dataGridView1.Font, FontStyle.Bold),
  127.                                 e.MarginBounds.Width).Width),
  128.                                 e.MarginBounds.Top - e.Graphics.MeasureString("Customer Summary",
  129.                                 new Font(new Font(dataGridView1.Font, FontStyle.Bold),
  130.                                 FontStyle.Bold), e.MarginBounds.Width).Height - 13);
  131.  
  132.                             //Draw Columns                 
  133.                             iTopMargin = e.MarginBounds.Top;
  134.                             foreach (DataGridViewColumn GridCol in dataGridView1.Columns)
  135.                             {
  136.                                 e.Graphics.FillRectangle(new SolidBrush(Color.LightGray),
  137.                                     new Rectangle((int)arrColumnLefts[iCount], iTopMargin,
  138.                                     (int)arrColumnWidths[iCount], iHeaderHeight));
  139.  
  140.                                 e.Graphics.DrawRectangle(Pens.Black,
  141.                                     new Rectangle((int)arrColumnLefts[iCount], iTopMargin,
  142.                                     (int)arrColumnWidths[iCount], iHeaderHeight));
  143.  
  144.                                 e.Graphics.DrawString(GridCol.HeaderText,
  145.                                     GridCol.InheritedStyle.Font,
  146.                                     new SolidBrush(GridCol.InheritedStyle.ForeColor),
  147.                                     new RectangleF((int)arrColumnLefts[iCount], iTopMargin,
  148.                                     (int)arrColumnWidths[iCount], iHeaderHeight), strFormat);
  149.                                 iCount++;
  150.                             }
  151.                             bNewPage = false;
  152.                             iTopMargin += iHeaderHeight;
  153.                         }
  154.                         iCount = 0;
  155.                         //Draw Columns Contents                
  156.                         foreach (DataGridViewCell Cel in GridRow.Cells)
  157.                         {
  158.                             if (Cel.Value != null)
  159.                             {
  160.                                 e.Graphics.DrawString(Cel.Value.ToString(),
  161.                                     Cel.InheritedStyle.Font,
  162.                                     new SolidBrush(Cel.InheritedStyle.ForeColor),
  163.                                     new RectangleF((int)arrColumnLefts[iCount],
  164.                                     (float)iTopMargin,
  165.                                     (int)arrColumnWidths[iCount], (float)iCellHeight),
  166.                                     strFormat);
  167.                             }
  168.                             //Drawing Cells Borders 
  169.                             e.Graphics.DrawRectangle(Pens.Black,
  170.                                 new Rectangle((int)arrColumnLefts[iCount], iTopMargin,
  171.                                 (int)arrColumnWidths[iCount], iCellHeight));
  172.                             iCount++;
  173.                         }
  174.                     }
  175.                     iRow++;
  176.                     iTopMargin += iCellHeight;
  177.                 }
  178.                 //If more lines exist, print another page.
  179.                 if (bMorePagesToPrint)
  180.                     e.HasMorePages = true;
  181.                 else
  182.                     e.HasMorePages = false;
  183.             }
  184.             catch (Exception exc)
  185.             {
  186.                 MessageBox.Show(exc.ToString());
  187.             }
  188.         }
  189.  
  190.     }
  191. }
Mar 23 '15 #1
0 3294

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Yoshitha | last post by:
Hi I am working on a web project. I have a InstallerClass in my project. While making setup ( using web setup template) for this web application, I have added a userinterface with 4...
0
by: K B | last post by:
Hi again, I have a gridview, when I get the selecteditem.cells for a column, if the database column is Null or Empty, and I assign that to my web form text control, the control reads "&nbsp;"...
6
by: Ron | last post by:
Hi, I know Access allows for easy construction of a report setup to print labels from a table/query, etc. I've done that one. It works pretty well for what I need. However, is there an...
1
by: ad | last post by:
I use GridView in a Web Form. When the data is empty, it only who the EmptyDataText or EmptyDataTempate, and all the field is invisible How can I show all the field when the data is empty?
2
by: ronchese | last post by:
I'm noticing this since I started developing a website project: my session variables are getting empty! I did a real easy test, and I checked my session variable is getting empty!! What is...
1
by: chetuashish | last post by:
I am importing a excel sheet data into a data set using C#.net in a web application i m using following connection string "DSN=Excel Files;DBQ=C:\\Inetpub\\wwwroot\\uninumberusa\\CDR\\Intellicenter...
13
by: Rocky86 | last post by:
Hi people pls help me having some trouble with the SQL I try to test the SQL to get a value but it does not work this is my table name location: uid uname lat 1 Peter Collins ...
4
by: pradeep c | last post by:
I am new vb.net im am developing a stand alone application, can some one help me how to get and display datas which are in grid view to craystal report?
0
by: hiranmaie | last post by:
Hi, I have a gridview with more than 10 rows of data. I want to export these gridview values into an excel sheet along with the header names of excel sheet. I am not using a database here. I...
12
by: dorandoran | last post by:
I followed this link to add new record from gridview. So far it's good but I need to modify the last piece to edit the record that I will supply the record id. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.