2016-08-15 55 views
0

我设法从列表中的第一个项目中获取索引,但是从同一行我想从第二个项目中获取文本值列使用索引号。如何从C#中的索引获得子项目的文本值

// Gets the item from the listview 
var item = listview1.FindItemWithText(textbox4.Text); 

if (item != null) 
{ 
    // Gets the index number from that item 
    var index = listview1.Items.IndexOf(item).ToString(); 
    string secondValue = listview1.Items(index).SubItems(1).Text); 
    // secondVlaue should have the subitems txt value? 

} 

我真的尝试了一切,但它似乎并没有工作。

编辑:

应该有与SubItem的方法因为它看起来像这样在视觉:

index  Name:    Information: 
0  Harry   Harry hasn't been online for 7 days 
1  Jason   jason hasn't been online for 5 days 
2  Sarah   Sarah hasn't been online for 2 days 
3  Jack   Online 

我所知道的是,哈利= 0的指数和杰克的指数= 3 。因此,现在对于这些索引值,我想要获得Column“信息”的文本值,该索引与名称Column具有相同的索引。

这应该有可能吗?

+0

如果你说'listview1.Items(index + 1)'? – Rahul

+0

“索引”是一个数值吗?你可能不需要'ToString()'。 –

+0

我还建议您学习如何浏览MSDN帮助文档。它提供了许多关于每个类可用的方法以及如何使用它们的信息。 –

回答

1

试试这个:

这里索引被指定为一个字符串。使它作为一个整数,然后只secondValue会给任何输出。 var index = listview1.Items.IndexOf(item);

ListView类中没有名为SubItems的方法。如果你想要下一个项目意味着你需要给索引加1个值。

string secondValue = listview1.Items(index+1).ToString(); 
相关问题