Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27437

Get LV column index by column header text?

$
0
0
OK this works...,
Code:

' setup LV
ListView1.View = View.Details
ListView1.AllowColumnReorder = True
ListView1.Columns.Add("A")
ListView1.Columns.Add("B")
ListView1.Columns.Add("C")

' Get column index by column header text
Dim i As Integer = GetLvColumnIndex(Me.ListView1, "B")
Debug.WriteLine(i)

Private Function GetLvColumnIndex(LV As ListView, HeaderText As String) As Integer
        For Each c As ColumnHeader In LV.Columns
                If c.Text = HeaderText Then
                        Return c.DisplayIndex
                End If
        Next c
        Return -1
End Function

But isn't there a simpler way to do the same thing?
I tried it like this, no syntax errors,..
Code:

Dim j As Integer = Me.ListView1.Columns("B").DisplayIndex
Dim i As Integer = Me.ListView1.Columns.Item("B").DisplayIndex

But both fail to run, Object reference not set to an instance of an object.

Viewing all articles
Browse latest Browse all 27437


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>