2014-12-12 63 views
1

我有與列的數目可變開始,我用插入它一個ListView和項目的數組列表如下:如何通過列表視圖細胞遍歷C#

for (int x = 0; x < arrayList.Count; x++) 
      { 
         ListViewItem item = new ListViewItem("IF"); 
         item.SubItems.Add("ID"); 
         item.SubItems.Add("EX"); 
         item.SubItems.Add("MEM"); 
         item.SubItems.Add("WB"); 
         for (int k = 5; k < ccNum; k++)//Set rest of row cells "" 
          item.SubItems.Add(""); 
         listView5.Items.Add(item); 
      } 

我想,隨着下一行的每一次迭代都會移至右側的x + 1個空值。但我似乎無法得到它的工作,我必須手動這是不切實際的考慮ArrayList中添加條件語句,並輸入他們也不是一成不變的:

    if (x == 1) 
        { 
         ListViewItem item = new ListViewItem("IF"); 
         item.SubItems.Add("ID"); 
         item.SubItems.Add("EX"); 
         item.SubItems.Add("MEM"); 
         item.SubItems.Add("WB"); 
         for (int k = 5; k < ccNum; k++) 
          item.SubItems.Add(""); 
         listView5.Items.Add(item); 
        } 
        else if(x==2) 
        { 
         ListViewItem item = new ListViewItem(""); 
         item.SubItems.Add("IF"); 
         item.SubItems.Add("ID"); 
         item.SubItems.Add("EX"); 
         item.SubItems.Add("MEM"); 
         item.SubItems.Add("WB"); 
         for (int k = 5; k < ccNum; k++) 
          item.SubItems.Add(""); 
         listView5.Items.Add(item); 
        } 
        else if(x==3) 
        { 
         ListViewItem item = new ListViewItem(""); 
         item.SubItems.Add(""); 
         item.SubItems.Add("IF"); 
         item.SubItems.Add("ID"); 
         item.SubItems.Add("EX"); 
         item.SubItems.Add("MEM"); 
         item.SubItems.Add("WB"); 
         for (int k = 5; k < ccNum; k++) 
          item.SubItems.Add(""); 
         listView5.Items.Add(item); 
        } ..etc 

如果我嘗試訪問的下一行單元格,並設置它像下面這樣:

ListViewItem item = new ListViewItem("IF"); 
         item.SubItems.Add("ID"); 
         item.SubItems.Add("EX"); 
         item.SubItems.Add("MEM"); 
         item.SubItems.Add("WB"); 
         for (int k = 5; k < ccNum; k++) 
          item.SubItems.Add(""); 
         listView5.Items.Add(item); 

         listView5.Items[x + 1].SubItems[x].Text = ""; 

我得到一個運行時錯誤:

InvalidArgument=Value of '2' is not valid for 'index'.

是否與ListView控件或與此有關的任何其他方法來實現它更簡單的方法?

+0

是不是應該是'if(x == 7)'或者'7'是否是'3'? – petelids 2014-12-13 00:00:15

+0

是的,應該是3.我犯了一個錯誤。 – 2014-12-13 00:17:47

+0

你有__很好的循環___在最後或主循環中添加空白子項。 __Why__你在開始時不插入類似的循環嗎? - 另一個問題沒有意義。什麼是x?這是怎麼發生的? – TaW 2014-12-13 01:37:52

回答

0

如果我理解正確的話,你可以用另一個循環,增加了空分項目實現這一點:

for (int x = 0; x < arrayList.Count; x++) 
{ 
    //if x is 0 it's the first iteration so we want "IF"; otherwise we want "" 
    ListViewItem item = new ListViewItem(x == 0 ? "IF" : ""); 
    //add the rest of the empty sub-elements with a loop 
    for (int y = 1; y < x; y++) 
    { 
     item.SubItems.Add(""); 
    } 
    item.SubItems.Add("ID"); 
    item.SubItems.Add("EX"); 
    item.SubItems.Add("MEM"); 
    item.SubItems.Add("WB"); 
    for (int k = 5; k < ccNum; k++)//Set rest of row cells "" 
     item.SubItems.Add(""); 
    listView5.Items.Add(item); 
} 
+0

非常感謝。這適用於我! @petelids – 2014-12-15 23:19:32

0

您正遇到一個非常常見的數組邊界問題。讓我告訴你一個代碼示例,以幫助...

static void Main(string[] args) 
    { 
     var tempList = new List<string>(); 
     tempList.Add("1"); 
     tempList.Add("2"); 
     int x; 
     for (x = 0; x < tempList.Count; x++) 
     { 
      //do something here 
     } 

     //value of x after loop 
     Console.WriteLine(x); 
     //the value of x is 2. 

     try 
     { 
      Console.WriteLine(tempList[x]); //lets try to read the value here 
     }catch(Exception e){ 
      Console.WriteLine("nope, not happening"); 
     } 

     //to drive this point further home... let me show you what you did... 
     tempList.Add("random"); 
     try 
     { 
      Console.WriteLine(tempList[x+1]); //lets try to read the value here after we added an element 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine("nope, not happening"); 
     } 
     Console.ReadLine(); 
    }