2012-06-01 29 views
0

我有一個gridview複選框在我的代碼隱藏頁面。功能是我需要使用複選框選擇要刪除的記錄,然後單擊刪除按鈕。我使用下面的代碼來做到這一點..但是,當我選擇最後一行時,它不會被刪除。相反,它拋出IndexOutOfRange/System.FormatException ..Gridview中的IndexOutOfRange/System.FormatException

誤差以這條線

CheckBox chkb = (CheckBox)gvAll.Rows[i].Cells[0].FindControl("chk"); 


     for (int i = 0; i < count; i++) 
     { 
      CheckBox chkb = (CheckBox)gvAll.Rows[i].Cells[0].FindControl("chk"); 
      if (chkb.Checked == true) 
      { 
       string name = gvAll.Rows[i].Cells[3].Text; 
       if (!(name.Equals(System.DBNull.Value))) 
       { 
        a.delete(name); 
       } 
      } 
     } 

這是一個緊迫的問題拋出。請幫忙..

+1

a.delete中的'a'是什麼 –

+0

'count'是如何到達的? – V4Vendetta

+0

a是另一個類的對象.. – user1080139

回答

1

foreach怎麼樣?

 foreach(GridViewRow row in gvAll.Rows) 
    { 
     CheckBox chkb = (CheckBox)row.Cells[0].FindControl("chk"); 
     if (chkb.Checked == true) 
     { 
      string name = row.Cells[3].Text; 
      if (!(name.Equals(System.DBNull.Value))) 
      { 
       a.delete(name); 
      } 
     } 
    } 
+0

試過..仍然得到最後一行的異常錯誤 – user1080139