views:

40

answers:

3

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 with string parameters? I don't want to use index it is too confusing

A: 

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

Andrea Parodi
Hi the post is tagged as VB
geocine
Sorry, my apologies... what error are you getting?
Andrea Parodi
+2  A: 

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.

Rob Lowther
A: 

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
Tim Murphy