Click here to Skip to main content
15,888,037 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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
Updated 15-Feb-13 20:00pm
v2

1 solution

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

VB
  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
  Loop

  If (pageLine >= lpp) And (_curRow <= _linesReq) Then
	page += 1
	pageLine = 1
	e.HasMorePages = True
	Exit For
  Else
	e.HasMorePages = False
        Exit For
  End If
Next
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900