2015-12-07 55 views
0

我从阵列中添加项目的顶部为一个ListView象下面这样:将现有项目到ListView

for (int i = 0; i < dataKeyList.Count; i++) 
{ 
    CallTabLv.Items.Add(new { Label = " " + keyArray[i], Value = valueArray[i] }); 
} 

如果keyArray[i]包含一个名为CustomerName一个项目,我怎么能和它的价值转移到列表顶部?

编辑:

让说:

string[] arr1 = new string[] { "one", "two", "three" }; 
string[] arr2 = new string[] { "1", "2", "3" }; 

for (int i = 0; i < arr1.Length; i++) 
{ 
    listView.Items.Add(new { C1 = arr1[i], C2 = arr2[i] }); 
} 

输出:

Col Col2 
----------- 
one  1 
two  2 
three 3 

我想移动的 “三3” 到列表的顶部,所以这将是如:

Col Col2 
----------- 
three 3 
two  2 
one  1 

有什么建议吗?

+1

你试过items.Insert方法吗? –

回答

1

除了添加项目,您可以使用[Items.Insert][1]在特定位置插入项目,它可以用于在n指数中更改现有项目的位置。

ListViewItem item= new ListViewItem("Test");//Define item here; 
if(!CallTabLv.Items.Contains(theItem)) 
{ 
    CallTabLv.Items.Insert(0,item); 
} 
else 
{ 
    n = CallTabLv.Items.IndexOf(item); 
    CallTabLv.Items.RemoveAt(n); 
    CallTabLv.Items.Insert(0, item); 
} 
+1

不错的一个老板! :) –

+0

嗨@非幸运,感谢您的解决方案,但我怎么知道我的项目的索引,因为它是从数组中获取,该项目将或不会在阵列中..? – YWah

+0

查看答案中的更新: –