2016-12-06 2 views
0

중복 된 질문처럼 보일 수 있습니다. (이 오류에 대해 묻는 다른 많은 질문을 보았습니다.) 그러나 나는 답을 찾을 수 없습니다. 제가 겪고있는 문제를 설명합니다. 오류가 내가 읽은 바로는추가 정보 : InvalidArgument = '0'의 값이 'index'에 유효하지 않습니다.

invList.SelectedItems[0].Text //invList is a ListView 

호출에 의해 유발되며,이 명령에 의해 자극 오류가 시도 비어있는 목록에 대한 액세스 (또는 배열?) ListView에있는 선택한 항목의 표시한다. 그러나 내가 액세스하려고 시도하는 ListView는 명령 실행시 항목으로 채워집니다.

방법 # 1 : 여기서 I는이 명령 실행되는 방법이다의 deleteItem는

public bool deleteItem() 
    { 
     if (invList.SelectedItems.Count == 0) //if no item selected... 
     { 
      MessageBox.Show("Error: No item selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
      return false; 
     } 
     else 
     { 
      DialogResult dialogResult = MessageBox.Show(
       "Are you sure you want to delete item: " + invList.SelectedItems[0].Text + "?", 
       "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); 
      if (dialogResult == DialogResult.Yes) 
      { 
       for (int i = 0; i < itemRecords.Count; i++) 
       { 
        if ((itemRecords[i].id.ToString() == invList.SelectedItems[0].Text) && (itemRecords[i].practice == clinicName)) 
        { 
         itemRecords.Remove(itemRecords[i]); 
         listRefresh(); 
        } 
       } 
       return true; //return true to indicate that data has been edited 
      } 
      return false; // return false to indicate that nothing has changed 
     } 
    } 

방법 # 2에 updateItem

public bool updateItem() 
    { 
     if (invList.SelectedItems.Count == 0) //if no item selected 
     { 
      MessageBox.Show("Error: No item selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
      return false; 
     } 
     else 
     { 
      for (int i = 0; i < itemRecords.Count; i++) 
      { 
       //if id == the id of the selected item 
       if((itemRecords[i].id.ToString() == invList.SelectedItems[0].Text) && (itemRecords[i].practice == this.Text)) 
       { 
        ItemAddition itemAddition = new ItemAddition(itemRecords, itemRecords[i], this); 
        itemAddition.ShowDialog(); 
       } 
      } 
      return true; 
     } 
    } 

listRefresh :

 public void listRefresh() 
    { 
     invList.Items.Clear(); 
     loadItems(); 
    } 

loadItems :

 private void loadItems() 
    { 
     foreach (Record r in itemRecords) 
     { 
      if (r.practice == clinicName) 
       invList.Items.Add(r.ToString()); 
     } 
    } 

첫 번째 메서드가 호출 될 때 오류가 호출되지만 두 번째 메서드가 호출 될 때는 오류가 호출되지 않습니다. 이 불일치는 내 혼란의 이유입니다. 이 오류가 첫 번째 방법에서만 발생하는 이유가 있습니까?

+0

listRefresh 메소드의 코드를 추가 할 수 있습니까? – Damith

+0

@Damith 물론, 그것을 추가했습니다. – AustinC

+0

'for (int i = 0; i

답변

0

항목을 제거한 후 새로 고침 방법을 이동하십시오. 항목을 이동하고 다시 바인딩하면 선택 항목이 손실됩니다.

for (int i = 0; i < itemRecords.Count; i++) 
    { 
     if ((itemRecords[i].id.ToString() == invList.SelectedItems[0].Text) && (itemRecords[i].practice == clinicName)) 
     { 
      itemRecords.Remove(itemRecords[i]); 
      //listRefresh(); 
     } 
    } 

listRefresh();     
return true; 
+0

답변을 보았을 때 제 문제에 대한 해결책을 찾았습니다. 이것은 정확히 잘못된 것입니다. 당신의 도움을 주셔서 대단히 감사합니다. – AustinC