2011-08-12 41 views
0

我想刪除ListView中除第一列以外的整個ListViewItems。我有一個方法,但它有時拋出ArgumentRangeException,我無法找到原因。刪除第一列以外的整個ListViewItems

private void ListViewClear() 
    { 

      for (int i = 0; i < lstKullanicilar.Items.Count; i++) 
      { 
       if (lstKullanicilar.Items[i].SubItems.Count != 1) 
       { 
        lstKullanicilar.Items[i].SubItems.RemoveAt(1); 
        lstKullanicilar.Items[i].SubItems.RemoveAt(2); 
        lstKullanicilar.Items[i].SubItems.RemoveAt(3); 
        lstKullanicilar.Items[i].SubItems.RemoveAt(1); 
        lstKullanicilar.Items[i].SubItems.RemoveAt(1); 
       } 
      } 

回答

1

嘗試事端這樣的:

for (int i = 0; i < lstKullanicilar.Items.Count; i++) { 
    while(lstKullanicilar.Items[i].Count > 1){ 
     lstKullanicilar.Items[i].SubItems.RemoveAt(1); 
    } 
} 

與您的代碼的問題可能是,你有子項,集合中的項目數量可變的。根據你展示的代碼,你必須在子項目集合中至少有6個項目,因爲沒有得到一個指責異常。

+0

項目[我]還沒有得到一個屬性計數,所以我改變了這樣> lstKullanicilar.Items [i] .SubItems.Count> 1和你的代碼工作。 –

+0

非常感謝。 –