Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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
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++;

C#
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.
 
Share this answer
 
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
your break stops the foreach, not the while loop
so for the while state
C#
Boolean stoptotalProcess = false;

before the break; state
stoptotalProcess = true;

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


then the processings stops
 
Share this answer
 
Comments
zakirox123 8-Feb-13 6:20am    
nothing use :( help me out
Herman<T>.Instance 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)



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