如果我达到20多条记录,无限循环打印页面? :( 请帮我 [英] endless loop print pages if i reached more than 20 records? :( please help me

查看:124
本文介绍了如果我达到20多条记录,无限循环打印页面? :( 请帮我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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();
}





代码块添加自Jibesh的评论[/编辑]

推荐答案



首先,你需要保持编码标准,括号和括号的纪律是有原因的。



如果你在其中添加额外的括号,那么while循环行会更具可读性,例如



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



又一次;



if(FieldValue.Length> 20)

{

FieldValue = FieldValue.Remove(20,FieldValue.Length - 20);

}



其次这是你的问题:



长度30是指数0到29



因此,如果您的长度为30,并且您要求从索引20中删除21计数,那么您将有9个额外字符;然后你要求从中删除10(30 - 20 = 10)。



这会抛出异常并且你没有捕获异常因此下面的这两个调用是

CurrentRecord ++;

count ++;

从未被调用过。所以你的CurrentRecord和Count将永远为零,循环将永远。



最后你的电话应该是



if(FieldValue.Length> 20)

{

FieldValue = FieldValue.Remove(20,FieldValue.Length - 21);

}



如果我是你,我会使用异常处理程序。





问候

Jegan
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


你正在突破你的,而尽快循环as CurrentRecord 等于 RecordsPerPage ,因此您不会处理剩余的记录。您还有 if 语句看起来已断开连接。
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.


这篇关于如果我达到20多条记录,无限循环打印页面? :( 请帮我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆