Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0 C#3.0 C# C#4.0
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 8-Feb-13 9:03am
Edited 8-Feb-13 9:38am
Jibesh16.6K
v3
Comments
Sergey Alexandrovich Kryukov at 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
richcb at 8-Feb-13 14:08pm
   
what is your "count" variable set to?
zakirox123 at 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 at 8-Feb-13 14:18pm
   
where you put this codes in your application? inside paint method?
zakirox123 at 8-Feb-13 14:25pm
   
i am using separate function and after that calling inside print method. :(
jibesh at 8-Feb-13 14:35pm
   
may i know what are you trying to do so that i may suggest a better way of doing it.
zakirox123 at 8-Feb-13 14:50pm
   
i just want to add 20 records per page from gridview, if it reach more than 20 then balance record should be added in the second page
Marco Bertschi at 8-Feb-13 14:31pm
   
I added the source sample which you have posted as comment to your Original post.
jibesh at 8-Feb-13 14:38pm
   
there is some correction in the updated code. I made it right. thanks
zakirox123 at 8-Feb-13 14:40pm
   
i just stop max 20 records no more pages if add even more :(
QuantumHive at 8-Feb-13 14:41pm
   
Why don't you just set a breakpoint before the while loop and see what happens?
zakirox123 at 8-Feb-13 14:44pm
   
stops max @20 no more pages even 25 records added
int count = 0;
 
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++;
}
 
//StopReading
if (count >0 )
{
//StopReading = true;
SetInvoiceTotal(g);
}
g.Dispose();
}
DinoRondelly at 8-Feb-13 18:22pm
   
What are itemgridview.Rows.Count and RecordsPerPage set at?
zakirox123 at 9-Feb-13 0:37am
   
RecordsPerPage = 20; itemgridview.Rows.Count max rows in item gridview
shiny13 at 9-Feb-13 2:28am
   
There's something definitely wrong in your looping logic. Why do you have 2 nested loops? Explain what the loops are for.
zakirox123 at 9-Feb-13 2:55am
   
one is to count records per page and the other one is for count rows in gridview inorder to add FieldValue
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 289
1 DamithSL 225
2 OriginalGriff 175
3 Peter Leow 150
4 Afzaal Ahmad Zeeshan 95
0 OriginalGriff 7,355
1 DamithSL 5,254
2 Sergey Alexandrovich Kryukov 4,942
3 Maciej Los 4,906
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2014
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