Click here to Skip to main content
Sign Up to vote bad
good
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:23

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

2 solutions

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:22
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:45
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:16
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:24
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:29
because an object cannot be convert to bool
Richard MacCutchan - 8 Feb '13 - 11:14
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:19
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:25
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:33
again fails :( if (count<20) { e.HasMorePages = false; } else e.HasMorePages = true;
Richard MacCutchan - 8 Feb '13 - 13:40
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:49
if (itemgridview.Rows.Count
zakirox123 - 8 Feb '13 - 13:35
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
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:20
nothing use :( help me out
digimanus - 8 Feb '13 - 6:29
show code how it looks now
zakirox123 - 8 Feb '13 - 6:37
//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
Your Filters
Interested
Ignored
     
0 OriginalGriff 213
1 Sergey Alexandrovich Kryukov 159
2 Richard MacCutchan 150
3 Maciej Los 136
4 Tadit Dash 110
0 Sergey Alexandrovich Kryukov 10,264
1 OriginalGriff 7,917
2 CPallini 4,181
3 Rohan Leuva 3,522
4 Maciej Los 3,125


Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 8 Feb 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid