2013-03-27 50 views
3

我有一個ListView1的其中顯示2列表列表視圖顯示錯誤

List<string> pths; 
List<string> rec; 

public void Disp() 
     { 

      DisplayListInColumns(listView1, pths, 0); 
      DisplayListInColumns(listView1, rec, 1); 
     } 
private static void DisplayListInColumns(ListView listView, List<string> values, int columnIndex) 
     { 
      for (int index = 0; index < values.Count; index++) 
      { 
       while (index >= listView.Items.Count) 
       { 
        listView.Items.Add(new ListViewItem()); 
       } 
       ListViewItem listViewItem = listView.Items[index]; 
       while (listViewItem.SubItems.Count <= columnIndex) 
       { 
        listViewItem.SubItems.Add(new ListViewItem.ListViewSubItem()); 
       } 
       listViewItem.SubItems[columnIndex].Text = values[index]; 
      } 
     } 

我現在用的是全球列表進行更改,並在ListView1的顯示,但C#的WinForms後,才用戶單擊apply_button將更改保存(在xml中)。

編輯和添加詳細信息按鈕工作正常,完美顯示。但是,當我刪除數據時,我會引發錯誤。

以下是刪除操作:

//Configuration - DELETE button 
     private void button6_Click(object sender, EventArgs e) 
     { 
      string select = null; 

      if (listView1.SelectedItems.Count > 0) 
      { 
       select = (listView1.SelectedItems[0].Text); 
      } 
      int count = listView1.SelectedItems[0].Index; 

      if (select != null) 
      { 
       pths.RemoveAt(count); 
       rec.RemoveAt(count); 
       string s = String.Join("; ", pths.ToArray()); 
       string r = String.Join("; ", rec.ToArray()); 
       //MessageBox.Show(s); 
       //MessageBox.Show(r);         
      } 
      Disp(); 
     } 

我想試了幾次後,我用食指去錯了。即使在刪除後,調試時,我得到的listView.Items.Count = 5. 我猜計數仍然是5(樣本-5列表中的字符串)時刪除它應該減少到4和索引0-3相應。 我正在以下eror

ArgumentOutOfRangeException at pths.RemoveAt(count) 
Index was out of range. Must be non-negative and less than the size of the collection. 
Parameter name: index 

作爲替代我試圖pths.Remove(select);,但沒有解決這個問題。

任何幫助,將不勝感激。謝謝

+0

您希望在ListView指數在列表<>的指數相匹配。當你刪除一個項目時,這將停止工作。既然你從List <>中刪除它,但不是ListView。所以最終「count」會比List <>中的最後一個索引大。 KABOOM。 – 2013-03-27 18:47:20

+0

謝謝@HansPassant我明白這個錯誤,只是困惑於一個合適的解決方案。 – 2013-03-27 19:04:55

+0

我不清楚爲什麼你在ListView中留下一個已刪除的項目。刪除它也將是簡單的解決方案。如果你需要離開它,那麼你還需要一個將listview項目映射到列表項目的字典。 – 2013-03-27 19:10:13

回答

0

聽起來像是你只需要重新加載列表...

//Clear it 
listView1.Items.Clear(); 
//reload it. 
listView1.Refresh(); 
+0

像魅力一樣工作......謝謝。我也要感謝mike-c,併爲他們的投入做好準備。 – 2013-03-28 12:06:20

2

更改您的if語句

if (select != null) 

這個

if(!string.IsNullOrWhiteSpace(select)) 

您的文字屬性將不爲空,這將是空字符串,所以你當你認爲進入該節你不是。

編輯:

根據您的意見,我要爲你指點該解決方案代替,像這樣的東西替換整個刪除功能:

private void button6_Click(object sender, EventArgs e) 
{ 
    foreach (ListViewItem eachItem in listView1.SelectedItems) 
    { 
    listView1.Items.Remove(eachItem); 
    if (pths.Any(o => o == eachItem.Text)) 
    { 
     pths.Remove(eachItem.Text); 
    } 
    if (rec.Any(o => o == eachItem.Text)) 
    { 
     rec.Remove(eachItem.Text); 
    } 
    } 
} 

您可能需要eachItem.Value相反,但我認爲.Text會起作用。

注: 這個答案我絕對剛剛從這裏複製(我不採取信貸這一解決方案):

Remove the Selected Item From ListView

+0

我更改了if語句:最初列表顯示,並在刪除第一項後最後一項是副本,整個列表剛剛向上移動。 – 2013-03-27 19:02:09

+0

您想要刪除項目,還是僅刪除選定項目。 – 2013-03-27 19:07:51

+0

只刪除選定的項目。用戶選擇一個項目(我已禁用多選)並單擊刪除。所選項目應從列表中刪除,並在列表視圖中進行更新。 Disp()只是遍歷顯示內容的列表 – 2013-03-27 19:22:07

1

你需要嘗試從List將其取出,以驗證您的SelectedItem第一。

private void button6_Click(object sender, EventArgs e) 
    { 
     string select = (listView1.SelectedItems.Count > 0) ? (listView1.SelectedItems[0].Text) : null; 
     if (!string.IsNullOrWhiteSpace(select)) 
     { 
      listView1.BeginUpdate(); 
      pths.Remove(select); 
      rec.Remove(select); 
      listView1.EndUpdate(); 

      string s = String.Join("; ", pths.ToArray()); 
      string r = String.Join("; ", rec.ToArray());      
     } 
     Disp(); 
    } 
+0

我也試過這個解決方案,我可以從列表這是pths和rec刪除項目,因爲當測試時,我顯示字符串s和r,它給了我刪除選定項目的列表。只是該列表視圖沒有正確更新。 – 2013-03-27 19:03:49

+0

更新瞭解決方案... ListView使用了什麼樣的集合? (pths或rec)也是,它怎麼沒有正確更新? – 2013-03-27 19:12:59

+0

這是一個字符串的集合。 'List pths = new List (xml.xmlreader(「C:\\ product.txt」,「Name」)); 列表 rec =新列表(xml.xmlreader(「C:\\ product.txt」,「Recurse」));' – 2013-03-27 19:22:28