简体   繁体   English

如何在C#中通过索引从子项获取文本值

[英]How to get the text value from a subitem by index in C#

I have managed to get the index from the very first item in the column in my listview, but from that same line I want to get the text value from the second Column by using the index number. 我设法从列表视图中列的第一项中获取索引,但是从同一行中,我想使用索引号从第二列中获取文本值。

 // 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?

}

I really tried everything, but it doesn't seem to work. 我真的尝试了一切,但是似乎没有用。

EDIT: 编辑:

There should be a method with SubItem cause it looks like this in visual: 应该有一个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

What I know is that the index of Harry = 0 and the index of Jack = 3. So now with those index values I want to obtain the text value of the Column 'Information' that has the same index as the name Column . 我知道的是,Harry = 0的索引和Jack = 3的索引。因此,现在,利用这些索引值,我想获得与名称Column索引相同的Column '的文本值。

This should be possible? 这应该可能吗?

Try this : 尝试这个 :

Here the index is assigned as a string .Make it as an integer then only secondValue will give any output . 此处的索引被分配为字符串。将其设为整数,则只有secondValue会给出任何输出。 var index = listview1.Items.IndexOf(item); var index = listview1.Items.IndexOf(item);

There is no method named SubItems in ListView class . ListView类中没有名为SubItems方法。 If you want the next item means you need to plus 1 value to the index . 如果需要下一项,则需要在索引上加上1的值。

string secondValue = listview1.Items(index+1).ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM