Click here to Skip to main content
15,868,007 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
int count = 0; 

int count = 0;

 while (CurrentRecord < RecordsPerPage && count < itemgridview.Rows.Count)
 {
            
   //foreach (DataGridViewRow row in itemgridview.Rows )
   //{
   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 (count>0 )
   {
    // StopReading = true;
    SetInvoiceTotal(g);
   }

   g.Dispose();
}


[Edit]Code Block added from Comment by Jibesh [/Edit]
Posted
Updated 8-Feb-13 8:38am
v3
Comments
Sergey Alexandrovich Kryukov 8-Feb-13 14:06pm    
This part of code does not reveal your problem. Show comprehensive code sample, short but complete. Use "Improve question"
—SA
Richard C Bishop 8-Feb-13 14:08pm    
what is your "count" variable set to?
zakirox123 8-Feb-13 14:11pm    
int count = 0;

while (CurrentRecord < RecordsPerPage && count < itemgridview.Rows.Count)
{

//foreach (DataGridViewRow row in itemgridview.Rows )
//{
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 (count>0 )
{
// StopReading = true;
SetInvoiceTotal(g);
}

g.Dispose();
}
Jibesh 8-Feb-13 14:18pm    
where you put this codes in your application? inside paint method?
zakirox123 8-Feb-13 14:25pm    
i am using separate function and after that calling inside print method. :(

Hi
first of all you need to maintain the discipline of coding standards, braces and brackets are for a reason.

The while loop line would be more readable if you add extra braces in it such as

while ((CurrentRecord < RecordsPerPage) && (count < itemgridview.Rows.Count))

And again;

if (FieldValue.Length > 20)
{
FieldValue = FieldValue.Remove(20, FieldValue.Length - 20);
}

Secondly here is your problem:

Length of 30 is index 0 to 29

So if you have length of 30 and you asked to remove from index 20 that is count 21 so you have 9 extra characters; then you are asking to remove 10 (30 - 20 = 10) from it.

this will throw exception and you are not catching the exception and therefore these two calls below are
CurrentRecord++;
count++;
never been called. So your CurrentRecord and Count will always be zero, and the loop will go forever.

Finally your call should be

if (FieldValue.Length > 20)
{
FieldValue = FieldValue.Remove(20, FieldValue.Length - 21);
}

If I were you I would make use of exception handler.


Regards
Jegan
 
Share this answer
 
You are breaking out of your while loop as soon as CurrentRecord equals RecordsPerPage, so you do not process the remaining records. You also have an if statement that looks disconnected.
 
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