【问题标题】:How to get the text value from a subitem by index in C#如何在C#中按索引从子项中获取文本值
【发布时间】:2016-12-22 05:19:09
【问题描述】:

我已经设法从列表视图中列中的第一项获取索引,但在同一行中,我想使用索引号从第二列获取文本值。

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

这应该可以吗?

【问题讨论】:

  • 如果你说listview1.Items(index + 1)呢?
  • index 是数值吗?你可能不需要ToString()
  • 我还建议您学习如何浏览 MSDN 帮助文档。它提供了大量关于每个类可用的方法以及如何使用它们的信息。

标签: c# listview indexing indexof subitem


【解决方案1】:

试试这个:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-06
    • 2021-03-08
    • 1970-01-01
    • 2011-07-10
    • 1970-01-01
    相关资源
    最近更新 更多