繁体   English   中英

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

[英]How to get the text value from a subitem by index in 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

我知道的是,Harry = 0的索引和Jack = 3的索引。因此,现在,利用这些索引值,我想获得与名称Column索引相同的Column '的文本值。

这应该可能吗?

尝试这个 :

此处的索引被分配为字符串。将其设为整数,则只有secondValue会给出任何输出。 var index = listview1.Items.IndexOf(item);

ListView类中没有名为SubItems方法。 如果需要下一项,则需要在索引上加上1的值。

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

暂无
暂无

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

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