Click here to Skip to main content
12,510,189 members (54,770 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#2.0 C#3.0 C C# C#4.0
private void SetInvoiceData(Graphics g, System.Drawing.Printing.PrintPageEventArgs e)
{
    // Set Invoice Table:
    string FieldValue = "";
    int CurrentRecord = 0;
    int RecordsPerPage = 20; // twenty items in a page
    decimal Amount = 0;
    bool StopReading = false;
 
    // Set Table Head:
    int xItems = leftMargin;
    CurrentY = CurrentY + InvoiceFontHeight;
 
    g.DrawString("Items", InvoiceFont, BlueBrush, xItems, CurrentY);
    int xQty = xItems + (int)g.MeasureString("Items", InvoiceFont).Width + 4;
 
    g.DrawString("Qty", InvoiceFont, BlueBrush, xQty, CurrentY);
    int xPrice = xQty + (int)g.MeasureString("Qty", InvoiceFont).Width + 72;
 
    g.DrawString("Price", InvoiceFont, BlueBrush, xPrice, CurrentY);
    int xSubTotal = xPrice + (int)g.MeasureString("Price", InvoiceFont).Width + 4;
 
    g.DrawString("SubTotal", InvoiceFont, BlueBrush, xSubTotal, CurrentY);
    //int xSpecialization = xAddress + (int)g.MeasureString("Specialization", InvoiceFont).Width + 4;

    // g.DrawString("Discount", InvoiceFont, BlueBrush, xSpecialization, CurrentY);

    AmountPosition = xSubTotal + (int)g.MeasureString("SubTotal", InvoiceFont).Width + 4;
    //g.DrawString("Extended Price", InvoiceFont, BlueBrush, AmountPosition, CurrentY);

    // Set Invoice Table:
    CurrentY = CurrentY + InvoiceFontHeight + 8;
 

 
    while (CurrentRecord < RecordsPerPage)
    {
        foreach (DataGridViewRow row in itemgridview.Rows)
        {
            FieldValue = row.Cells[0].Value.ToString();
            // FieldValue = itemgridview.Rows["items"].ToString();
            g.DrawString(FieldValue, InvoiceFont, BlackBrush, xItems, CurrentY);
 

            //FieldValue = dr["itmqty"].ToString();
            FieldValue = row.Cells[1].Value.ToString();
 
            // if Length of (Product Name) > 20, Draw 20 character only
            if (FieldValue.Length > 20)
 
                FieldValue = FieldValue.Remove(20, FieldValue.Length - 20);
            g.DrawString(FieldValue, InvoiceFont, BlackBrush, xQty, CurrentY);
 
            //FieldValue = String.Format(dr["itemprice"].ToString());
            FieldValue = row.Cells[2].Value.ToString();
            g.DrawString(FieldValue, InvoiceFont, BlackBrush, xPrice, CurrentY);
 
            //FieldValue = dr["subtotal"].ToString();
            FieldValue = row.Cells[3].Value.ToString();
            g.DrawString(FieldValue, InvoiceFont, BlackBrush, xSubTotal, CurrentY);
 

            CurrentY = CurrentY + InvoiceFontHeight;
 

            if (itemgridview.Rows.Count.ToString() == null)
            {
                StopReading = true;
                break;
            }
            else
            {
                CurrentRecord++;
            }
        }
    }
Posted 7-Feb-13 22:23pm
Comments
boogac 8-Feb-13 4:29am
   
it must be the code is always not going to else {CurrentRecord++} block..did you try to debug mode ?
zakirox123 8-Feb-13 4:37am
   
ya i tried in debug mode it just stop there if (itemgridview.Rows.Count.ToString()==null)
{
StopReading = true;
break;
// return;
}

CurrentRecord++;
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

if (itemgridview.Rows.Count.ToString() == null)
What exactly do you expect this test to do? Rows.Count[^] returns an integer value of the number of rows in the collection so use that value to test against.
  Permalink  
Comments
zakirox123 8-Feb-13 6:22am
   
i used in this way to but no use

if (itemgridview.Rows.Count == 0)// or <1
{

StopReading = true;
break;
// return;
}

CurrentRecord++;
Richard MacCutchan 8-Feb-13 6:45am
   
How do you expect that value to be zero if you have data in the grid? You need to compare your current record value against the number of rows, to see if you have reached the end.
zakirox123 8-Feb-13 7:16am
   
this will get the last cell value because we are using while loop
if (row.Cells[3].Value.ToString()=="0")
{
//condition
}
Richard MacCutchan 8-Feb-13 7:24am
   
Why are you converting values to strings, and then comparing the converted item to string constants? Use proper integer comparisons and maybe some of your code will become clearer.
zakirox123 8-Feb-13 7:29am
   
because an object cannot be convert to bool
Richard MacCutchan 8-Feb-13 11:14am
   
What are you talking about? You are taking an integer value and converting it to a string and then doing comparisons that will not give the answers you expect. I would suggest you get hold of a C# programming reference and do some serious studying.
zakirox123 8-Feb-13 13:19pm
   
finally i solved the problem like these, but the problem arise again when i reached more than 20 records it again starting endless loop :(

while (CurrentRecord < RecordsPerPage && count < itemgridview.Rows.Count)
{
DataGridViewRow row = itemgridview.Rows[count];
FieldValue = row.Cells[0].Value.ToString();
// FieldValue = itemgridview.Rows["items"].ToString();
g.DrawString(FieldValue, InvoiceFont, BlackBrush, xItems, CurrentY);


//FieldValue = dr["itmqty"].ToString();
FieldValue = row.Cells[1].Value.ToString();

// if Length of (Product Name) > 20, Draw 20 character only
if (FieldValue.Length > 20)

FieldValue = FieldValue.Remove(20, FieldValue.Length - 20);
g.DrawString(FieldValue, InvoiceFont, BlackBrush, xQty, CurrentY);

//FieldValue = String.Format(dr["itemprice"].ToString());
FieldValue = row.Cells[2].Value.ToString();
g.DrawString(FieldValue, InvoiceFont, BlackBrush, xPrice, CurrentY);

//FieldValue = dr["subtotal"].ToString();
FieldValue = row.Cells[3].Value.ToString();
g.DrawString(FieldValue, InvoiceFont, BlackBrush, xSubTotal, CurrentY);

CurrentY = CurrentY + InvoiceFontHeight;

CurrentRecord++;
count++;
}


if (CurrentRecord < RecordsPerPage)
{
e.HasMorePages = false;
}
else
e.HasMorePages = true;

//StopReading
if (count !=+1 )
{
// StopReading = true;
SetInvoiceTotal(g);
}
Richard MacCutchan 8-Feb-13 13:25pm
   
if (CurrentRecord < RecordsPerPage)
This test is wrong, you will always break out of the upper loop when these two are equal, even when you have more rows to process. You should be checking if count is less than total number of rows.
zakirox123 8-Feb-13 13:33pm
   
again fails :(
if (count<20)
{
e.HasMorePages = false;
}
else
e.HasMorePages = true;
Richard MacCutchan 8-Feb-13 13:40pm
   
Why are you comparing it to 20? You want to know whether it is less than the total number of rows. If it is less, then you have more pages to display, if it is equal or greater then you have processed all the rows, and have no more pages.
zakirox123 8-Feb-13 13:49pm
   
if (itemgridview.Rows.Count
zakirox123 8-Feb-13 13:35pm
   
i am sorry to disturb you i am not a expert developer i am student and i am working for my project "hotel management system" please help
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

your break stops the foreach, not the while loop
so for the while state
Boolean stoptotalProcess = false;
before the break; state
stoptotalProcess = true;

before the end brackt } of the while state
if (stoptotalProcess)
   break;

then the processings stops
  Permalink  
Comments
zakirox123 8-Feb-13 6:20am
   
nothing use :( help me out
digimanus 8-Feb-13 6:29am
   
show code how it looks now
zakirox123 8-Feb-13 6:37am
   
//skipped some code
bool StopReading = false;
Boolean stoptotalProcess = false;
while (CurrentRecord < RecordsPerPage)
{


foreach (DataGridViewRow row in itemgridview.Rows)
{
FieldValue = row.Cells[0].Value.ToString();
// FieldValue = itemgridview.Rows["items"].ToString();
g.DrawString(FieldValue, InvoiceFont, BlackBrush, xItems, CurrentY);


//FieldValue = dr["itmqty"].ToString();
FieldValue = row.Cells[1].Value.ToString();

// if Length of (Product Name) > 20, Draw 20 character only
if (FieldValue.Length > 20)

FieldValue = FieldValue.Remove(20, FieldValue.Length - 20);
g.DrawString(FieldValue, InvoiceFont, BlackBrush, xQty, CurrentY);

//FieldValue = String.Format(dr["itemprice"].ToString());
FieldValue = row.Cells[2].Value.ToString();
g.DrawString(FieldValue, InvoiceFont, BlackBrush, xPrice, CurrentY);

//FieldValue = dr["subtotal"].ToString();
FieldValue = row.Cells[3].Value.ToString();
g.DrawString(FieldValue, InvoiceFont, BlackBrush, xSubTotal, CurrentY);


CurrentY = CurrentY + InvoiceFontHeight;


if (itemgridview.Rows.Count==0)
{
stoptotalProcess = true;
StopReading = true;
break;
//StopReading = true;

// return;
}


CurrentRecord++;

}
if (stoptotalProcess)
break;

}

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
Web02 | 2.8.160929.1 | Last Updated 8 Feb 2013
Copyright © CodeProject, 1999-2016
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