2014-12-12 64 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(); 
    }