Click here to Skip to main content
13,044,705 members (63,698 online)
Rate this:
Please Sign up or sign in to vote.
See more: , +
Hi Everyone.......

I want to print a data-set in windows form application . In my dataset there are 5 rows and 3 columns , and i want to print two rows per pages .means there will be 3 pages to print whole data-set. My code is
float CurrentX = 20;//This is for X axis
float CurrentY = 100; //This is for Y axis
 int linesPerPage = 2;
 int count = 0;
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            foreach (DataRow Dr in ds.Tables[0].Rows)
                foreach (DataColumn dc in ds.Tables[0].Columns)
                    e.Graphics.DrawString(Dr[dc].ToString(), DefaultFont, Brushes.Black, CurrentX, CurrentY);
                    CurrentX = CurrentX + 50;//Increment  of X axis by 50
                CurrentY = CurrentY + 100;//Increment of Y axis by 100 for new  row
                CurrentX = 20; //Initialize the X axis for start the same position 
                count = count + 1;//Increment the count 
                if (count > linesPerPage)
                    e.HasMorePages = true;                                 
                else e.HasMorePages = false;

But here number of pages in going to infinity and result is not coming . actually
e.HasMorePages logic is not cleared to me, i try to implement the logic but every time i failed. Where is the mistake , please suggest some things .

Thank You....
Posted 15-Feb-13 19:26pm
Updated 15-Feb-13 20:00pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Here is what I do. The important stuff is highlighted.
You need to work out how much space you have on the page: Dim lpp As Integer = CInt(Math.Floor((e.MarginBounds.Bottom - y) / 18))

and ensure that you track where you are in the collection: _curRow and pageLine

  Dim pageLine As Integer = 1
  Dim lpp As Integer = CInt(Math.Floor((e.MarginBounds.Bottom - y) / 18))
  For i As Integer = _curRow To _linesReq
  Do While (pageLine <= lpp) And (_curRow <= _linesReq)

    ' Print some stuff
    y += h
    _curRow += 1
    pageLine += 1
  If (pageLine >= lpp) And (_curRow <= _linesReq) Then
	page += 1
	pageLine = 1
	e.HasMorePages = True
	Exit For
	e.HasMorePages = False
        Exit For
  End If

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 16 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100