开发者

How to access ListView subitem by Key?

I am trying to access data from list view using columnheader name but I get errors

LVProduct.FocusedItem.SubItems("Name").Text

So, how do you use the function wi开发者_JAVA技巧th string parameters? I don't want to use index it is too confusing


If you're wanting to get the SubItem based on a column, this is the easiest way.

LVProduct.FocusedItem.SubItems(columnHeader1.Index).Text 

columnHeader1 would be the name of the ColumnHeader you're wanting the SubItem from.


When you create the sub items, you have to set the Name property to be that of the column in which it resides.

The [...] accessor looks up the Name field in the ListViewSubItem class.

Without that set, the ["Name"].text operation you're carrying out will return a null pointer type error.

Or, to put it another way, the search by key does not search by column name. It searches according to the value that you set in SubItem.Name when you create the subitem.


When creating the sub items you need to set the name property. For example:

listViewItem.SubItems.Add(
    New ListViewSubItem With {.Name = "Name", .Text = "yes"}
)

Then you can use your existing code:

LVProduct.FocusedItem.SubItems("Name").Text


Your supposed to use c# "indexer" . You have to call using sqare brackets:

LVProduct.FocusedItem.SubItems["Name"].Text

If you want to know more on indexer, look at this link on MSDN

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜