Search code examples
c#visual-studiolistviewlistviewitem

C# How do I get the ColumnHeader of a ListViewItem?


I haven't found the answer anywhere. How do I get the ColumnHeader of a given ListViewItem?

The context is that I am inside the ListViewColumnSorter (custom code). I have access to two ListViewItem's, how can I get the ColumnHeader? I need to access the column's tag which contains information I put in there through the VS designer.


Solution

  • The ListViewItem spans all columns in the ListView (e.g. lvItem.SubItems[0] is column 1, lvItem.SubItems[1] is column 2, etc. You should know the column number you're sorting by. Then you can trace back through the ListView property:

    ColumnHeader = lvItem.ListView.Columns[columnNumber];